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

Save them in a ring buffer:
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
  • Comment on Re: Get specified number of previous line text when encountered a text match in a file
  • Download Code