How to Use Sed Command in Linux with Examples

SED abbreviated as 'Stream Editor' is a powerful text stream editor that can perform a lot of functions on file that is searching, find and replace and insertion. The sed command is regular expression aware command, means it can understand the regular expressions provided to it.

Though, SED is primarily or mainly used for text substitution. With the help of SED, we can edit files without opening it, which will be much faster and quicker to find and replace something in a file, rather than opening the file and changing it.

In this tutorial, we'll learn SED command with easy-to-use and understand examples in our Linux shell.

Sed Command Syntax

Sed command syntax is pretty simple to understand but is very powerful in use. Here is the basic syntax of SED as follows :

sed OPTIONS... [SCRIPT] [INPUTFILE...]

Consider the below text file ('content.txt') as an input for the sed examples command.

$ cat > content.txt

lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.

Here are the most highly used sed examples as follows.

1. Replacing String (words or characters)

Sed can be used to find and replace a string (words or characters) on the input. The option s is used for replacing a string. By default, sed command only replaces the first occurrence of the string in a line.

The following command replaces the word 'lorem' with 'Lorem' in the 'content.txt' file for the first occurrence in each line:

$ sed 's/lorem/Lorem/' content.txt

Here 'lorem' is the search string and the 'Lorem' is the replacement string.

Output
$ sed 's/lorem/Lorem/' content.txt
Lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.

2. Replace all the occurrence of a string in a file

To search and replace all the occurrences of a string in a file use /g (global replacement) flag.

In the following command, /g flag is used to replace all the occurrences of the string 'lorem' with 'Lorem' in the file 'content.txt'.

$ sed 's/lorem/Lorem/g' content.txt
Lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
learn more about dummy text.

3. Replacing the nth occurrence of a pattern in a line

The sed command can be used with /1,/2 or n (any number) to replace the first, second or nth occurrence of a string in a line.

The following command replaces the second (2) occurrence of the word 'lorem' with 'Lorem' in each line.

$ sed 's/lorem/Lorem/2' content.txt

lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
learn more about dummy text.

Similar to tr command, sed can replace all occurrences of characters in set1 with the corresponding characters in set2 using y/set1/set2/.

$ echo 'a for apple' | sed -e 'y/af/AF/
Output
A For Apple

4. Replace from nth occurrence to all the occurrences in each line

We can use a combo of /1,/2 or n (any number) and /g to replace all the patterns from the nth occurrence of a string in each line.

In the following example, all occurrences of the string 'lorem' will be replaced with 'Lorem' from the 2nd occurrence in each line:

$ sed 's/lorem/Lorem/2g' content.txt
lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
learn more about dummy text.

5. Search and replace a string on a specified line number

With sed, we can restrict the function only to replace the string on a specific line number.

In the following command, we can find that only the second line is replaced by the string 'Lorem'.

$ sed '2 s/lorem/Lorem/' content.txt

lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.

6. Display partial text of a file

Using sed command you can view some portion of a file rather than the whole file. The -n option is used to suppress printing all content and p option is used print specific lines.

The following command will print lines from 2 to 4:

$ sed -n 2,4p content.txt
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.

7. Display all contents except particular lines

Sed command allows displaying all contents of the file except particular lines. In the following command display all content on the file 'content.txt' except lines 1 and 2:

$ sed 1,2d content.txt
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.

8. Display all lines except pattern matching line

We can use the following sed command to delete the line that has the search pattern that is mentioned. In the output, the line with "learn" is deleted because it matches the search pattern.

$ sed '/learn/d' content.txt
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.

9. Display replaced lines

In order to print the replaced lines, we make the use of /p flag along with -n.

In the output, we can see there is not the last line because it didn't have the search pattern 'lorem'.

$ sed -n 's/lorem/Lorem/p' content.txt

Lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.

More examples of 'p' and 'n' flag

Print first line

$ sed -n '1p' content.txt

Print range of line

$ sed -n '1,4p' content.txt

Print multiple lines

$ sed -n '1p;4p' content.txt

Print a file

$ sed -n 'p' content.txt

Print contains regex or word

$ sed -n '/RedHat/p' content.txt

Print if contains digits

$ sed -n '/[0-9]/p' content.txt

Match regex and replace

$ sed -n 's/^L/Matched--&/p' content.txt
Output
Matched--Lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.
Matched--Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
Matched--Lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.

Use sed like grep

The following sed command will search for user 'root' in '/etc/passwd' file:

$ sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

10. Combine SED commands

If we have to perform multiple sed expressions then we need to use e option to chain the sed commands.

Let's take an example, to replace all the occurrence of the "lorem"  with "Lorem" and deleting the line that matches the search pattern.

$ sed -e 's/lorem/Lorem/g' -e '/learn/d' content.txt

Lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.

11. Insert blank line after each line

Using G option you can insert single or multiple blank lines in a file.

The following command inserts single blank line between the lines:

$ sed G content.txt

lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.

lorem ipsum is highly used by designers. lorem ipsum is great for developers.

lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.

learn more about dummy text.

To insert two blank lines run the below command:

$ sed 'G;G' content.txt

lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.

lorem ipsum is highly used by designers. lorem ipsum is great for developers.

lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.

learn more about dummy text.

11. Edit and create a backup copy of orginal file

Sed allows to edit a file and same time create a backup copy of the original file. We use 'i.<file extension>' for backup file name and -e editing.

The following command creates a backup of original file 'content.txt' as 'content.txt.bak'

$ sed -i.bak -e 's/lorem/Lorem/g' content.txt
Results
$ cat content.txt
Lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
learn more about dummy text.
$ cat content.txt.bak
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.

12. Deleting a line with a pattern

Using this sed command, you can delete a line starting with a particular string & ending with another string. In the following output, the line with starting 'lorem' and ending with 'text.' is deleted:

$ sed -e 's/^lorem.*text.$//g' content.txt
Output
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.

13. Appending String to lines

We can execute the following command in order to add some content before every line using regex.

In the following output, we can see 'Here' is added in front of every line.

$ sed -e 's/.*/Here &/' content.txt
Here lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.
Here lorem ipsum is highly used by designers. lorem ipsum is great for developers.
Here lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
Here learn more about dummy text.

Insert string before each line:

$ sed 'i \inserted line' content.txt
inserted line
Lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.
inserted line
Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
inserted line
Lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
inserted line
learn more about dummy text.

Append string after each line:

$ sed 'a \Appended line' content.txt
Lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.
Appended line
Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
Appended line
Lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
Appended line
learn more about dummy text.
Appended line

14. Extract usernames from /etc/passwd file

Sed command can be easily used to get all the list of all usernames from '/etc/passwd' file.

The following sed command will extract all the usernames available linux:

$ sed 's/\([^:]*\).*/\1/' /etc/passwd
root
daemon
bin
sys
sync
man
lp
news
uucp
proxy
backup
list
gnats
nobody
systemd-network
systemd-resolve
syslog
messagebus
uuidd
debian-spamd
admin

15. Print line without commented lines (#) & empty lines

Print without commented (#) lines and empty lines are simple with sed.

We can execute the following command to print all lines which don't contain comment symbol (#) and all the empty lines.

$ sed -e 's/#.*//;/^$/d' content.txt

To remove only commented lines:

$ sed -e 's/#.*//' content.txt

16. Extract all ip addresses with sed from a string

Let's check how to extract IP addresses using sed command with regex. I have added a few IP addresses to 'content.txt' for testing.

$ cat content.txt
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
122.65.49.2
221.11.165.233
219.158.9.97
219.158.19.137

Now let use regex to extract IP address from 'content.txt' file as follows:

$ sed '/\n/!s/[0-9.]\+/\n&\n/;/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\n/P;D' content.txt
122.65.49.2
221.11.165.233
219.158.9.97
219.158.19.137

I found another easy command for getting the same results, check below:

$ sed -n '/[0-9]/p' content.txt.bak
122.65.49.2
221.11.165.233
219.158.9.97
219.158.19.137

We can also combine sed with other command using pipes (|), check below example:

$ ip addr | sed -n '/inet/p' | sed -e 's/  */ /g' | cut -d ' ' -f3
127.0.0.1/8
::1/128
75.208.225.10/24
2600:3c02::e03c:92ff:fe60:d7e2/64
fe80::f03c:92ef:fe60:d7e2/64

17. Redirect and write to a file

The w flag can be used to write the output to a file, instead of printing on the standard output. The w command requires a filename to which the output will be written.

The following sed command will redirect output to a new file 'rootpwd.txt':

$ sed -n '/root/w rootpwd.txt' /etc/passwd
$ cat rootpwd.txt
root:x:0:0:root:/root:/bin/bash

Conclusion

In this Linux command tutorial, we learned the basic syntax of sed command and with the most commonly and heavily used sed examples. One can use sed in bash or shell scripting to perform different automated tasks in text manipulation and sed operations in Linux shell. With this tutorial, one can easily understand sed and start implementing right away when required. If you have any questions, suggestions, feedback please write them in the comment box below.

Bobbin Zachariah 8:57 am

Comments

Your email address will not be published. Required fields are marked *