Ron Newman <[EMAIL PROTECTED]> writes: > But this won't solve the original problem, which was how to write a > Perl script that provides N lines of context before and after a 'grep' match. > Your program processes each line one at a time, and doesn't store a > buffer of lines.
Try this:
my $N = 10;
my $regex = shift;
my @buffer = ();
while (<>) {
push(@buffer, $_);
next if $. < $N * 2;
if ($buffer[$N] =~ /$regex/) {
print "@buffer\n";
}
}
Adjust the print statement to taste. :-)
--
John Abreau / Executive Director, Boston Linux & Unix
ICQ 28611923 / AIM abreauj / JABBER [EMAIL PROTECTED] / YAHOO abreauj
Email [EMAIL PROTECTED] / WWW http://www.abreau.net / PGP-Key-ID 0xD5C7B5D9
PGP-Key-Fingerprint 72 FB 39 4F 3C 3B D6 5B E0 C8 5A 6E F1 2C BE 99
"The early bird catches the worm, but the second mouse gets the cheese."
msg01883/pgp00000.pgp
Description: PGP signature
