Use sed to find and replace

grep is to find what sed is to replace.

Say you noticed a few typos in a file. Say you typed imposible instead of impossible a few times in a file named "stuff.txt".

You can fix all occurrences using this one-liner:

sed 's/imposible/impossible/g' stuff.txt


Often it's more helpful to sed across multiple files.

sed -i 's/1.0.1/"1.0.1"/g' *.php


That's one command I used to add quotes around 1.0.1, which I forgot to do. Alas, it's not recursive.



Notes
1. sed tutorial.
2. find and replace with sed.
3. about.com on sed.
4. Very clear examples.

Posted 01/20/2010
blog comments powered by Disqus