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; }
If you really only need the first 2 lines after the last "====" line, then maybe this is a good start:
updated: duplicate line removed.my @last_lines; while (<>) { if ($_ eq "D : Error Occurred\n") { print @last_lines,$_; last; } if (/===/) { @last_lines =(); } elsif (@last_lines < 2) { push @last_lines,$_; } }
|
|---|