# In fat crayon mode for your pleasure
# spoofing your big log file handle as a little array for brevity
my @logs = ();
for(1..20){push(@logs, $_)}
my $lookBack = 2; # the number of lines behind the match you want
my $lookAhead = 2; # the number of lines ahead the match you want
my $matchReg = '9'; # what your matching on (can be inlined)
my $matched = 0; # flag when we have a match
my @buffer = (); # your lookBack buffer
# Iterate through the logs looking for match
foreach my $line (@logs) {
# Feed the buffer
push(@buffer, $line);
# Trim the buffer
shift(@buffer) if $#buffer > ($lookBack-1);
# Do stuff if this line matches
if ($line =~ /$matchReg/) {
# Print the buffer
for(@buffer){print "lookback: $_\n"}
# Wave a flag we have a match
$matched = $lookAhead;
# Nothing else to see here, move along
next;
}
# Still working the lookAhead from a prior match?
if($matched){
# one less match
--$matched;
print "LookAhead: $line\n";
}
}