in reply to Remove line above matching criteria

One way to do this (discard N lines previous to a match) is to keep a buffer of seen lines which is at least N long. You print lines which overflow out normally and then just discard the buffer on a match. Lastly, remember to print anything in the buffer at the end.

In code:

#!/usr/bin/perl use strict; use warnings; # We don't really need an array for one line, but it seems # conceptually nicer (and generalises more easily) my $max_buffer_size = 1; my @buffer; my $line; while ($line = <ARGV>) { push @buffer, $line; # Replace the pattern match with your criterion if this # isn't right. @buffer = () if $line =~ /^.id/; if (scalar @buffer > $max_buffer_size) { print shift @buffer; } } print @buffer;