in reply to grab 'n' lines from a file above and below a /match/
You're trying to do that by manipulating $., but that won't read more lines. Since you want to keep lines from before the match, you'll need to buffer them. Here's one way,
my $n = 10; # , say my @lines; { local $_; while (<LOG>) { push @lines, $_; if (/c9391b56-b174-441b-921c-7d63/) { # push @lines, (<LOG>)[0..$n-1] and last; # better, while (<LOG>) { push @lines, $_; last if @lines > 2*$n; } last; } else { shift @lines while @lines > $n; } } }
Update: improved the code to not read the rest of the file after a match is found.
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: grab 'n' lines from a file above and below a /match/
by barathbr (Scribe) on Sep 16, 2004 at 23:07 UTC | |
by Zaxo (Archbishop) on Sep 16, 2004 at 23:22 UTC | |
by barathbr (Scribe) on Sep 16, 2004 at 23:44 UTC |