Linux/Unix – Replacing Text with sed
Finding text within a file is sometimes the first step towards replacing text. Editing streams of text is done using the sed command. The sed command is actually a fullblown scripting language. For the examples in this chapter, we cover basic text replacement with the sed command.
If you are familiar with text replacement commands in vi, sed has some similarities. In the following example, you would replace only the first occurrence per line of marvin with giovanni. Here, sed takes its input from a pipe, while sending its output to stdout (yourscreen):
$ cat myfile.txt | sed s/marvin/giovanni/
Adding a g to the end of the substitution line, as in the following command, causes every occurrence of marvin to be changed to giovanni. Also, in the following example, input is directed from the file myfile.txt and output is directed to mynewfile.txt:
$ sed s/marvin/giovanni/g < myfile.txt > mynewfile.txt
The next example replaces the first occurrences of of the text /home/msoto to /home2/mgsotoso from the /etc/passwd file. (Note that this command does not change that file, but outputs the changed text.) This is useful for the case when user accounts are migrated to a new directory (presumably on a new disk), named with much deliberation, home2. Here, we have to use quotes and backslashes to escape the forward slashes so they are not interpreted as delimiters:
$ sed ‘s/\/home\/msoto/\/home2\/mgsotoso/g’ < /etc/passwd
Although the forward slash is the sed command’s default delimiter, you can change the delimiter to any other character of your choice. Changing the delimiter can make your life easier when the string contains slashes. For example, the previous command line that contains a path could be replaced with either of the following commands:
$ sed ‘s-/home/msoto/-/home2/mgsotoso/-’ < /etc/passwd
$ sed ‘sD/home/msoto/D/home2/mgsotoso/D’ < /etc/passwd
In the first line shown, a dash (-) is used as the delimiter. In the second case, the letter D is the delimiter.
The sed command can run multiple substitutions at once, by preceding each one with -e. Here, in the text streaming from myfile.txt, all occurrences of francois are changed to FRANCOIS and occurrences of chris are changed to CHRIS:
$ sed -e s/marvin/MARVIN/g -e s/giovanni/GIOVANNI/g < myfile.txt
You can use sed to add newline characters to a stream of text. Where Enter appears, press the Enter key. The > on the second line is generated by bash, not typed in.
$ echo aaabccc | sed ‘s/b/\Enter
The trick just shown does not work on the left side of the sed substitution command. When you need to substitute newline characters, it’s easier to use the tr command.