[plug] Perl search and replace, a better way?

Jason Bainbridge JaseOne at myrealbox.com
Tue Oct 8 13:19:19 WST 2002


I took quite a while last night and this morning trying to find a good one line solution to recursively search and replace strings within specified files, I've come up with a command that I believe does everything that I want but I thought some of you gurus might know a better way.

What I came up with was the following (should be all one line obviously):

perl -pi.bak -e 's/foo/bar/g;' `find . -maxdepth 99 -type f -name "*.txt" -exec egrep -li "foo" {} \; | xargs`

(Replace foo with bar, in files matching *.txt, in the CWD, creating backup's of the original file, within pretty much the whole directory structure)

Most of the stuff I found on google didn't do the egrep step and just used the -print from the find command for the arguments to Perl, but that had the effect of creating backups of all the files returned by the find command and also reprinting their contents even if their wasn't a string to replace in the file so all *.txt files would be backed up and modified.

Is there any holes in my one liner or any adverse affects it may have?

Cheers,
Jason



More information about the plug mailing list