in reply to Get specified number of previous line text when encountered a text match in a file
my $rex = qr/Fifth/; my @last_four; while(<DATA>) { push @last_four, $_; shift @last_four if @last_four > 4; print @last_four if $_ =~ $rex; } __DATA__ First Second Third Forth Fifth Sixth
|
|---|