in reply to Get specified number of previous line text when encountered a text match in a file
However, Tie::File is not that efficient on large files.tie @l, "Tie::File", $file or die $!; for my $i (0 .. $#l) { if ($l[$i] =~ /Error Occurred/) { while($i > 0) { $i--; if ($l[$i] =~ /(A|B):\d+/) { print $l[$i]; exit; # or last } elsif ($l[$i] =~ /^====/) { exit; # or last } } } }
Another alternative could use a combination of a normal filehandle to go forward in the file, and File::ReadBackwards to go backward in the same file.
|
|---|