in reply to Delete 10 lines before matched line
#!/usr/bin/perl use strict; use warnings; my @buffer; my $buffer_size = 10; my $match = qr/toolerror/; # Read a file from STDIN. while ( my $line = <> ) { if ( $line =~ /$match/ ) { # Discard the buffered lines. @buffer = (); # Print the matched line. print $line; } else { # Store non matching lines in a buffer. push @buffer, $line; # Maintain the buffer at a max size. if ( @buffer > $buffer_size ) { print shift @buffer; } } } # Print any remaining buffered lines. print @buffer; __END__
--
John.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Delete 10 lines before matched line
by ikegami (Patriarch) on Mar 11, 2010 at 15:49 UTC | |
by jmcnamara (Monsignor) on Mar 11, 2010 at 16:47 UTC | |
by briggs (Initiate) on Mar 11, 2010 at 19:03 UTC | |
by ikegami (Patriarch) on Mar 11, 2010 at 19:31 UTC |