Script to delete a line from many files

Kevin Buettner plug-discuss@lists.plug.phoenix.az.us
Thu, 24 Apr 2003 18:40:13 -0700


On Apr 24,  5:01pm, Alan Dayley wrote:

> > You don't even need a script.  Just do:
> > 
> > perl -i -ne 'print unless /your regex here/' file1 file2 ...
> > 
> > If you want to create a backup file of each file modified, just do
> > (instead):
> > 
> > perl -i.bak -ne 'print unless /your regex here/' file1 file2 ...
> > 
> > Kevin
> 
> Thank you Kevin!  Just one more tweak would be cool.  I have many files 
> to process so what would it take to make the "file1 file2 ..." input 
> into a regular expresion too, or to automatically get the file list from 
> the ls command or something?

Well, you can do something like this:

    perl -i -ne 'print unless /your regex here/' `find . -regex something`

where ``something'' is your file matching regular expression.  BTW, note
the backquotes around the find command.  They're very important.

If you *really* have too many files, that won't work either (because
there are limits on the lengths of command lines), so instead do:

    find . -regex something | xargs perl -i -ne 'print unless /your regex here/'

xargs knows how long command lines can be.  It'll break up the output from
the find into reasonable chunks and feed it to multiple invocations of the
perl command.

Finally, if you want to do it all with a perl script, you can use a
script similar to the one I used a while back for making some changes
to gdb.  See:

    http://sources.redhat.com/ml/gdb-patches/2001-03/msg00101.html

Take a look at the "find" invocation from this script.  As I recall, it
was looking for all of the ChangeLog entries.

Hmm... that one's a bit complicated.  This one might be better for
your purposes:

    http://sources.redhat.com/ml/gdb-patches/2000-05/msg00331.html

Kevin