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
    I dont see any output using it, I just tried it against a smaller text file and that doesnt give any output either ..

      Does it help to say print @lines; at the end?

      After Compline,
      Zaxo

        yes it does :) doh!! thanks