Replacing a String in a File with sed

Advertisement

Advertisement

sed -i 's/old string/new string/g' filename.txt

This is probably the most commonly used form of sed. Taking an existing file and replacing a string throughout the whole file. The -i flag tells sed to replace the string in place in the file. The s/ means search and replace, while the /g at the end means to do it globally through the whole line and not just the first occurence. It will, by default, run for each line already. You can also run the same replace on multiple files instead of a single file like log-2013-*.txt. Use http://www.gnu.org/software/sed/manual/sed.html for more info and examples on sed.

Advertisement

Advertisement