Using sed to perform text replacement
sed stands for stream editor. It is a very essential tool for text processing, and a marvelous utility to play around with regular expressions. A well-known usage of the sed command is for text replacement. This recipe will cover most of the frequently-used sed techniques.
How to do it…
sed can be used to replace occurrences of a string with another string in a given text.
It can be matched using regular expressions.
$ sed 's/pattern/replace_string/' fileOr:
$ cat file | sed 's/pattern/replace_string/'This command reads from
stdin.Note
If you use the
vieditor, you will notice that the command to replace the text is very similar to the one discussed here.By default,
sedonly prints the substituted text. To save the changes along with the substitutions to the same file, use the-ioption. Most of the users follow multiple redirections to save the file after making a replacement as follows:$ sed 's/text/replace/' file >newfile $ mv newfile file
However...