in reply to print line 5 lines previous to comaprison line!
if the file isn't big, read it all in an array, then when you find your match, save your location, and retrieve it directly from the array..
Update:Here it is, completely untested for bugs:
# assuming the file is already opened and has a <IN> filehandle my @temp; # and assuming that you won't match before the first 5 lines.. while (my $line = <IN>) { push @temp, $line; shift @temp if (scalar @temp > 5); if ($line =~ /foo/) { # we've got a match. print $temp[0]; } }
Update 2: This looks like davorg's more elegant solution.
|
|---|