Replacing all occurences of a string in a file with another

By Druss , 2 May, 2013

In Linux, replacing all instances of a string with another string is easy thanks to sed. A simple example is as follows:

To replace the string foo with the string bar in all .txt files:

sed 's#foo#bar#g' *.txt

This will replace the strings. However, it won't actually save the changes and will instead output the modified text to the screen. To retain the changes or, in other words, to perform the replacements in place within the file, use the -i switch:

sed -i 's#foo#bar#g' *.txt

Easy peasy Japanese.

Tags Old

All times are UTC. All content licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.