in reply to How can I print three lines before pattern match?
#!/usr/bin/perl use strict; my @file = <DATA>; for my $i (0 .. $#file) { if ( $file[$i] =~ /pattern/ ) { print "$file[$_]" for ($i-3 .. $i-1); } } __DATA__ log info line yada yada 0 log info line yada yada 1 log info line yada yada 2 log info line yada yada 3 log info line (pattern) 4 log info line yada yada 5
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How can I print three lines before pattern match?
by ikegami (Patriarch) on Aug 14, 2009 at 02:50 UTC | |
by bichonfrise74 (Vicar) on Aug 14, 2009 at 04:39 UTC | |
|
Re^2: How can I print three lines before pattern match?
by neurotoxx (Novice) on Aug 14, 2009 at 02:34 UTC | |
by QM (Parson) on Aug 14, 2009 at 03:02 UTC | |
by ikegami (Patriarch) on Aug 14, 2009 at 02:48 UTC |