in reply to upto match please, my one-lina!!
for perl, maybe like (not sure you want to slurp .. this just keeps an array of the last N lines):egrep -B4 /foo/ file
# put the line into the cache/queue; shorten the array if it's more t +han we want; print the array if current line mataches. perl -ne 'push @lines, $_; shift @lines if scalar(@lines)>4; print jo +in "", @lines if /foo/' file # if you don't want the current line printed as one of the 4, just rea +rrange a little: perl -ne 'print join "", @lines if /foo/; push @lines, $_; shift @lin +es if scalar(@lines)>4' file
use Tie::File; use Fcntl 'O_RDONLY'; my @array; tie @array, 'Tie::File', $ARGV[0], mode => O_RDONLY or die; foreach my $i ( 0 .. $#array ){ # no, doesn't handle index edge case of $i < 4 print join "", @array[$i-4,$i-1] if $array[$i] =~ /foo/; }
|
|---|