TIMTOWDI or here is one way to use $. ;-)
my $context = 4; my @buffer = ('') x $context; my $print_to = 0; my $match = qr/42/; while(<DATA>) { if ( m/$match/ ) { print @buffer; $print_to = $. + $context; @buffer = ('') x $context; } push @buffer,$_; shift @buffer; print if $. <= $print_to; }
cheers
tachyon
In reply to Re^2: grab 'n' lines from a file above and below a /match/
by tachyon
in thread grab 'n' lines from a file above and below a /match/
by barathbr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |