9

Replacing a String in a File with sed

 3 years ago
source link: https://www.devdungeon.com/content/replacing-string-file-sed
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Replacing a String in a File with sed

Submitted by NanoDano on Thu, 06/06/2013 - 23:08

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK