Archive

Posts Tagged ‘perl’

How to search for pattern in a file and remove the previous 2 lines including the line with pattern ?

April 4, 2011 Leave a comment

How to search for the pattern in a file and remove the previous 2 lines including the line with pattern ?

#!/usr/local/bin/perl

my @buffer;
while ( ) {
if (!/pattern/) {
push @buffer, $_;
} else {
push @buffer, $_;
delete @buffer[-3..-1]; ## Remove 2 lines above including pattern!
}
}
print @buffer;

Categories: Perl, Perl - Examples Tags: ,