in reply to Re: grab 'n' lines from a file above and below a /match/
in thread grab 'n' lines from a file above and below a /match/
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
|
|---|