Nik Nik - 1 year ago 63
Perl Question

remove lines after pattern in multiple files

I have the following command

find ./ -type f -exec perl -pi -e 's/<pattern>//g' '{}' \;

it works well and remove the pattern, but how can I remove the pattern, the line it is on, and
lines after?

Answer Source

It's more brute force than elegant, but I think this will remove the line with the matching pattern and the line following it.

find ./ -type f -exec perl -i -e '$x=0 ; while (<>) {if ($_ =~ /<pattern>/) { $x++; next; }; if ($x) { $x=0; next; }; print "$_";}' {} \;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download