in reply to Get specified number of previous line text when encountered a text match in a file

my @last_lines; while (<>) { if ($_ eq "D : Error Occurred\n") { print @last_lines,$_; last; } push @last_lines,$_; shift @last_lines if @last_lines > 4; }
Never mind. that's not what you want at all.

If you really only need the first 2 lines after the last "====" line, then maybe this is a good start:

my @last_lines; while (<>) { if ($_ eq "D : Error Occurred\n") { print @last_lines,$_; last; } if (/===/) { @last_lines =(); } elsif (@last_lines < 2) { push @last_lines,$_; } }
updated: duplicate line removed.
  • Comment on Re: Get specified number of previous line text when encountered a text match in a file
  • Select or Download Code