- 论坛徽章:
- 0
|
本帖最后由 黑色阳光_cu 于 2010-08-08 18:56 编辑
修正点小bug
sed -n '/NSValue/{h;:l n;//{G;h;$!bl};x;p;x;//q};p' sample.txt
黑色阳光_cu 发表于 2010-08-08 17:52 ![]()
等价的Perl写法:
- #!/bin/env perl
- use strict;
- use warnings;
- my @stack;
- while (my $line = <DATA>)
- {
- if (index($line, "NSValue") != -1 ... index($line, "NSValue") == -1)
- {
- if (index($line, "NSValue") != -1)
- {
- unshift(@stack, $line);
- }
- else
- {
- foreach my $line (@stack)
- {
- print $line;
- }
- @stack = ();
- print $line;
- }
- }
- else
- {
- print $line;
- }
- }
- foreach my $line (@stack)
- {
- print $line;
- }
- __DATA__
- NSArray *red = [NSArray arrayWithObjects:
- [NSValue valueWithCGPoint: ccp(0, 94.0)],
- [NSValue valueWithCGPoint: ccp(0, 44.0)],
- [NSValue valueWithCGPoint: ccp(0, 40.0)],
- [NSValue valueWithCGPoint: ccp(0, 15.0)],
- nil];
复制代码 or
- #!/bin/env perl
- use strict;
- use warnings;
- my @stack;
- while (my $line = <DATA>)
- {
- if (index($line, "NSValue") != -1)
- {
- unshift(@stack, $line);
- }
- else
- {
- if ($#stack > -1)
- {
- foreach my $line (@stack)
- {
- print $line;
- }
-
- @stack = ();
- }
-
- print $line;
- }
- }
- foreach my $line (@stack)
- {
- print $line;
- }
- __DATA__
- NSArray *red = [NSArray arrayWithObjects:
- [NSValue valueWithCGPoint: ccp(0, 94.0)],
- [NSValue valueWithCGPoint: ccp(0, 44.0)],
- [NSValue valueWithCGPoint: ccp(0, 40.0)],
- [NSValue valueWithCGPoint: ccp(0, 15.0)],
- nil];
复制代码 |
|