in reply to Print Lines above and below string
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); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Print Lines above and below string
by coding_new (Sexton) on Aug 10, 2011 at 15:39 UTC | |
Re^2: Print Lines above and below string
by ww (Archbishop) on Aug 10, 2011 at 22:19 UTC | |
Re^2: Print Lines above and below string
by Anonymous Monk on Mar 24, 2016 at 09:12 UTC | |
by Anonymous Monk on Mar 24, 2016 at 09:22 UTC |