Software Development with Linux

Quick find replace script

WED, 01 APR 2009

I've just say this nice and quick search & replace script at AskApache. If you scroll down, you'll get the one line command version, which is :

grep -H -R -l $1 $3 2>/dev/null | xargs -iFFF sed -i -e "s%${1}%${$2}%g" FFF
With :

  • $1 -> what you're searching

  • $2 -> what you want to put instead

  • $3 -> directory to search for

If you're typing this command directly on a command line, don't forget to quote the arguments!

Even if the article's author shows it for upgrading configuration file, this can be really helpful for anything kind of text file : XML, source code, etc.

You may also find the full blown shell script interesting. It's a nice example of what you can do in a shell script (colors, interactive user input, etc.)