To replace a text in a file, you can invoke sed as in the following example :
1 | % 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 :
1 | 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



















[...] If you want to see how to script a text replacement, check out my previous post about text replacement with sed. [...]
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