in reply to print line 5 lines previous to comaprison line!
Try this:
my @previous; while (<LINE>) { if ($_ eq "STRING TO COMPARE WITH\n") { print $previous[0]; last; } push @previous, $_; shift @previous if @previous > 5; }
Update: Thanks to jeroenes for spotting a couple of things that could be done better - I've incorporated your suggestions into the code.
Update 2: Thanks to davorg for noticing that print @previous prints out all of the last 5 lines, but pat asked for the line 5 lines previous to the one being searched. I've changed the print @previous line to read print $previous[0] instead.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: print line 5 lines previous to comaprison line!
by tachyon (Chancellor) on Jun 07, 2001 at 16:52 UTC |