Sed Command Examples In Linux

The sed command is a stream editor used in Linux and Unix operating systems popularly. The sed command is used to search, replace, edit, insert, delete, and similar text operations for single or multiple files via command-line interface in a programmatic way.

sed Command Syntax

The sed command syntax is like below where OPTIONS, SCRIPT, and INPUT_FILE can be specified.

sed OPTIONS SCRIPT INPUT_FILE
  • OPTIONS is used to set basic options.
  • SCRIPT is the most important part where different sed scripts can be specified for diffreent string operations.
  • INPUT_FILE is the file where the string operations will take.

The result is returned to the standard output and in order to make changes persistent, the output should be saved into a file.

Replace/Substituting String

One of the most used features of the sed command is replacing or substituting strings in a file. In the following example, we substitute “debian” with “ubuntu” for the file named “distro.txt”.

$ sed 's/debian/ubuntu/' distro.txt

Replace/Substituting nth Occurrence of String

A string or word can exist multiple times in a text. The previous example replaces all occurrences. But we can specify the occurrence number to replace. In the following example, we replace the “debian” with “ubuntu” for every 3rd occurrence in the “distro.txt” file.

$ sed 's/debian/ubuntu/3' distro.txt

Replace/Substituting From nth Occurrence To End of String

We can specify the starting occurrence number and replace it till the end. In the following example, we replace the “debian” with “ubuntu” starting from the 3rd occurrence till the end.

$ sed 's/debian/ubuntu/3' distro.txt

Replace String For Specfied Lines

By default, sed works for the whole file but we can specify the lines where the sed script can work. The line number can be used to make changes or replace them effectively. In the following example, we change “debian” with “ubuntu” for line number 5 for the file “distro.txt”.

$ sed '5 s/debian/ubuntu/' distro.txt

Print Only Replaced Lines

By default, the whole file is printed as sed output and if the file is very long it can be hard to review. The -n the option can be used to print only replaced or processed lines which makes review easier and faster.

$ sed -n 's/debian/ubuntu/' distro.txt

Replace String In the Specified Range of Lines

By default, the specified string is replaced for the complete file. But we can specify the line range where the replacement will be effective. The line range which consists of start and end line numbers is defined as the first part of the sed script. In the following example, we define the line range by using 5 as the start line number and 10 as the end line number.

$ sed '5,10 s/debian/ubuntu/' distro.txt

Delete Specified Line

In the following example, we delete a specific line by using d with the line number. For the following example, we delete line number 5.

$ sed '5d' distro.txt

Delete Last Line

The last line of the file can be deleted with the following sed script.

$ sed '$d' distro.txt

Delete Range of Lines

The d can be used to delete a range of lines where lines are specified before d by separating them with a command. In the following example, we delete line numbers between 5 and 10.

$ sed '5,10d' distro.txt

Delete Lines From Specidied Line Number To End

In the following example, we delete lines from starting line number 20 till the end.

$ sed '20,$d' distro.txt

Delete Matched Lines

We can set a search term and delete all matched lines completely like below. In the following example, we delete lines that contain the search term “ubuntu”.

$ sed '/ubuntu/d' distro.txt
See also  Linux less Command Tutorial

Leave a Comment