Alex Alex - 5 months ago 18
Perl Question

Remove multiple lines from multiple files multiple times

I have several files (

*.txt
) that I need to remove lines from. The files look like this:

This is a line to keep.
keep me too
START
some stuff to remove
other to remove
END
keep me!


And I want them to look like this:

This is a line to keep.
keep me too
keep me!


I've gotten this far:

perl -i -p0e 's/#START.*?END/ /s' file.txt


Which will remove the first instance of that from
file.txt
, but I can't figure out how to remove all instances from
file.txt
(and then how to apply this to all
*.txt
files?)

Answer

If what you show works for the first instance, all you should need to add is the /g flag to do all instances, and a shell glob to pick out all .txt files:

perl -i -p0e 's/#START.*?END/ /gs' *.txt