in reply to find a match and pull nth line before the match

Untested:
my $N = ...; # Number of lines before matching line. my $PATTERN = "..."; # What you want to match. my @buffer; while (my $line = <>) { if (@buffer < $N) { push @buffer, $line; next; } if ($line =~ /$PATTERN/) { print $buffer[0]; } shift @buffer; push @buffer, $line; }

Replies are listed 'Best First'.
Re^2: find a match and pull nth line before the match
by msensay (Novice) on Mar 14, 2012 at 19:31 UTC
    Thanks JavaFan. That's exactly what I needed.