in reply to grab 'n' lines from a file above and below a /match/
Well, you'll have to store the lines. And it's going to be tricky to correctly handle matches where the context overlaps, ie where one match follows less than 2n lines from the previous.
The easiest thing to do is use the toolbox: egrep -C n c9391b56-b174-441b-921c-7d63 GWSvc.log
Update: the following should work and handle all edge cases:
my @backlog; my $to_print = 0; my $context_size = 10; while(<>) { $to_print = 1 + $context_size if /c9391b56-b174-441b-921c-7d63/; push @backlog, $_; if( $to_print ) { print shift @backlog; --$to_print; } elsif( @backlog > $context_size / 2 ) { shift @backlog; } } print shift @backlog while @backlog and $to_print--;
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: grab 'n' lines from a file above and below a /match/
by tachyon (Chancellor) on Sep 17, 2004 at 03:55 UTC | |
|
Re^2: grab 'n' lines from a file above and below a /match/
by barathbr (Scribe) on Sep 16, 2004 at 22:50 UTC | |
by Aristotle (Chancellor) on Sep 16, 2004 at 23:04 UTC | |
by flogic (Acolyte) on Sep 17, 2004 at 18:14 UTC | |
|
Re^2: grab 'n' lines from a file above and below a /match/
by barathbr (Scribe) on Sep 16, 2004 at 23:29 UTC | |
by Aristotle (Chancellor) on Sep 16, 2004 at 23:40 UTC | |
|
Re^2: grab 'n' lines from a file above and below a /match/
by barathbr (Scribe) on Sep 16, 2004 at 23:15 UTC |