So you want the context of the matched string. You will need to keep a rolling window of the last few lines you've seen. Actually, if your input isn't huge, just slurp up into an array, like so (untested):
open LOGFILE, "<$file"; @lines = <LOGFILE>; for my $i (0..$#lines) { next unless $lines[$i] =~ /$string_to_find/i; my $a = $i - 5 < 0 ? 0 : $i - 5;; my $b = $i + 5 > $#lines ? $#lines : $i + 5; print $lines[$_i] for my $_i ($a..$b); }
In reply to Re: Print Lines above and below string
by thenaz
in thread Print Lines above and below string
by coding_new
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |