To replace a text in a file, you can invoke sed as in the following example :
% cat file.txt | sed -e 's/text/replacement/g' > result.txt
This will change all the occurences of “text” to “replacement” in “file.txt” and output the result in “result.txt”
Note : As suggested by Matthias from adminlife in the comments, if you wanted to do “in place” text replacement (that is modify the file without a temporary file in between), you can do the following :
sed -i ’s/text/replacement/g’ file.txt
For more complicated text manipulations you might consider moving to Perl, but sometimes you don’t need the sledge-hammer 🙂
You can do this more easily:
sed -i ‘s/text/replacement/g’ file.txt
The replacement will be done directly in file.txt 🙂
Matthias,
Thanks for passing by, and thanks for the comment : you’re right, it’s useful and I’ll add this as a note in the post.
Stephane