Linux sed Examples
In this post, we feature a comprehensive Linux sed Examples. Sed is basically a stream editor used for modifying files in unix or linux. It provides a nifty way to perform operations on files which can be passed around through pipes. Most people never learn its real power, they just simply use sed to replace text. You can do many things apart from replacing text with sed.
As mentioned earlier, sed is an editor that allows us to use pattern matching to search and replace within a file, or an input stream. This works by using Regular Expressions. By default, the results of any edits we make to a source file, or input stream, will be sent to STDOUT, the standard output. The original file will be unaffected by the edits.
Also the sed command can be incredibly useful when bootstrapping a new server, or machine, as it can easily be scripted. A common use case for sed is to script the editing of configuration files on a new server instance to facilitate the further setup of the needed environment for that machine.
In this article I will describe the capabilities of sed with examples. Consider the below file as input to our examples:
>cat example.txt I want to learn java. Learn java. Learn java java is the best java forever
1. Replacing string
Sed command is mostly used to replace the text in a file. The below sed command replaces the word “java” with “guava” in the file only for the first occurrence in each line:
>sed 's/java/guava/' example.txt
Here the “s” specifies the substitution operation. The “/” are delimiters. The “java” is the search pattern and the “guava” is the replacement string.
By default, the sed command replaces the first occurrence of the pattern in each line and it won’t replace next occurences.
2. Replacing the nth occurrence of a pattern in a line
Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word “java” with “guava” in a line:
>sed 's/java/guava/2' example.txt
3. Replacing all the occurrences of a pattern in a line
The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line:
>sed 's/java/guava/g' example.txt
4. Replacing from nth occurrence to all occurrences in a line
Use the combination of /1, /2, /n and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces from the second occurrence until the nth of the word “java” with the word “guava” in a line:
>sed 's/java/guava/2g' example.txt
5. Duplicating the replaced line with /p flag
The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.
>sed 's/java/guava/p' example.txt
The below image illustrates the execution of the first 5 sed commands:
6. Replacing string on a specific line number
You can restrict the sed command to replace the string on a specific line number. The below sed command replaces the string “java” only on the second line:
>sed '2 s/java/guava/' example.txt
7. Replacing string on a range of lines
You can specify a range of line numbers to the sed command for replacing a string. Here the sed command replaces the lines with range from 1 to 3. You may use the $ operator to indicate the last line in the file:
>sed '1,$ s/java/guava/' example.txt
8. Replacing on a line which matches a pattern
You can specify a pattern to the sed command to match in a line. If the pattern match occurs, then the sed command looks only for the string to be replaced and if it finds it, then it replaces the string. Here the sed command first looks for the lines which have the pattern “java” and then replaces the word “java” with “guava”.
>sed '/java/ s/java/guava/' example.txt
9. Deleting lines
You can delete the lines of a file by specifying the line number or a range of numbers:
>sed '2 d' example.txt
>sed '1,$ d' example.txt
10. Duplicating lines
You can use the sed command to print each line of a file two times:
>sed 'p' example.txt
The below image illustrates the execution of the previous 5 sed commands:
11. Changing the slash (/) delimiter
You can use any delimiter other than the slash. As an example if you want to change the web url to another url, using too many backslashes makes the sed command look awkward. In this case we can change the delimiter to another character as shown in the below example:
>sed 's_http://_www_' example.txt
12. Using & as the matched string
There might be cases where you want to search for the pattern and replace that pattern by adding some extra characters to it. In such cases & comes in handy. The & represents the matched string:
>sed 's/java/{&}/' example.txt
13. Using \1,\2 and so on to \9
The first pair of parenthesis specified in the pattern represents the \1, the second represents the \2 and so on. The \1,\2 can be used in the replacement string to make changes to the source string. As an example, if you want to replace the word “java” in a line with twice as the word like “javajava” use the sed command as below:
>sed 's/\(java\)/\1\1/' example.txt
14. Running multiple sed commands
You can run multiple sed commands by piping the output of one sed command as input to another sed command. Sed provides also an -e option to run multiple sed commands in a single sed command:
>sed 's/java/guava/' example.txt | sed 's/guava/python/'
>sed -e 's/java/guava/' -e 's/guava/python/' example.txt
15. Printing only the replaced lines
Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time:
>sed -n 's/java/guava/p' example.txt
The below image illustrates the execution of the previous 5 sed commands:
16. Using sed as grep
You can make sed command to work as similar to grep command. Here the sed command looks for the pattern “java” in each line of a file and prints those lines that have the pattern:
>grep 'java' example.txt
>sed -n '/java/ p' example.txt
17. Adding a line after a match is found.
The sed command can add a new line after a pattern match is found. The “a” command to sed tells it to add a new line after a match is found:
>sed '/java/ a "Add a new line"' example.txt
18. Adding a line before a match
The sed command can add a new line before a pattern match is found. The “i” command to sed tells it to add a new line before a match is found:
>sed '/java/ i "New line"' example.txt
19. Changing a line
The sed command can be used to replace an entire line with a new line if a match is found. The “c” command to sed tells it to change the line.
>sed '/java/ c "Change line"' example.txt
20. Transforming like tr command
The sed command can be used to convert the lower case letters to upper case letters by using the transform “y” option. In the below example the sed command transforms the alphabets “av” into their uppercase format “AV”
>sed 'y/av/AV/' example.txt
The below image illustrates the execution of the last 5 sed commands:
Man sed
We tried to present some basic examples of using the sed command. Of course you may always use the man command to find the full functionality of the sed command:
>man sed
In example 3, you say the /g replaces all occurrences of a pattern in a line, but your screen output shows java is replaced on all three lines of the example text file. Did you mean to say file instead of line?
Then, in example 4, you say from the nth to all occurrences of a pattern in a line, and the output shows only the 2nd and 3rd occurrence of java is replaced. Does this mean that /g is only global to a line when used in conjunction with another flag?
Dear Hackhound,
In the line means in each line, sorry if it was confusing, so for example 3 the global flag replaces all occurrences of the pattern in each line of the file. In example 4, 2g means replace the pattern in each line from the second occurrence and onwards. So it replaces the second and third occurrence in the first line, but nothing on 2nd and 3rd line as there is only one occurrence of the pattern in lines and 3.
item 11 seems to have html render error. for the syntax to change delimiter you have
>sed ‘s_http://_www_’ example.txt