Do you want to find and replace the string in all files matching grep?
perl -p -i -e 's/oldstring/newstring/g' `grep -ril searchpattern *`
Edit
Since this seems to be a pretty popular question that I would update.
Currently, I mainly use ack-grep as it is more user friendly. Thus, the specified command will be:
perl -p -i -e 's/old/new/g' `ack -l searchpattern`
To handle spaces in file names, you can run:
ack --print0 -l searchpattern | xargs -0 perl -p -i -e 's/old/new/g'
you can do more with ack-grep . Suppose you want to limit your search to HTML files only:
ack --print0 --html -l searchpattern | xargs -0 perl -p -i -e 's/old/new/g'
And if space is not a problem, it is even shorter:
perl -p -i -e 's/old/new/g' `ack -l --html searchpattern` perl -p -i -e 's/old/new/g' `ack -f --html`
armandino Jan 22 '09 at 22:57 2009-01-22 22:57
source share