Beginners Guide for Sed Command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The sed command is a stream editor that helps you search, find, replace, insert, and delete strings from the referenced text file without opening it.

It is similar to the AWK and grep commands that follow the regular expression approach to modify the string from the text file.

Tutorial Details

DescriptionSed (Stream Editor)
Difficulty LevelModerate
Root or Sudo PrivilegesMaybe
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitessed
Internet RequiredNo

Sample Text File

In this article, you will learn the sed command with the help of the following text file.

$ cat file.txt 

Output:

Sample file content

All the changes you made in this file will only be visible in the output; the file itself will not be modified; however, you can use the redirection symbol β€œ>>” to save the changes in a new file.

Find and Replace String from File

You can easily find a string from the file and replace it with something else, as shown.

$ sed 's/line/sentences/' file.txt 

The above command will replace all the β€œline” strings with β€œsentences” strings.

Find and replace the string in the text file

The above changes will not show up in the referenced text file, but you can use the redirection symbol to make a new text file with the changes.

$ sed 's/line/sentences/' file.txt >> newfile.txt

The above command will create and save the changes in a new text file with the name β€œnewfile.txtβ€œ.

Replacing the nth Occurrence of a Pattern in a Line

In the previous example, only the first occurrence of β€œline” was replaced with the β€œsentences” string. However, you can replace the string if it appears a second or third time.

$ sed 's/line/sentences/2' file.txt

Output:

Replace the string on a specific occurrence

The above command will replace the second instance of β€œline” with the string β€œsentences”.

$ sed 's/line/sentences/3' file.txt

Output:

Replace the string on the third occurrence

The above command will replace the third instance of β€œline” with the string β€œsentences”.

Replacing from nth Occurrence to All Occurrences in a Line

The following command will replace all the strings after the second occurrence of β€œline” with the β€œsentences” string.

$ sed 's/line/sentences/2g' file.txt

Output:

Replace all occurrences starting with the nth.

Replacing All the Occurrences of a Pattern in a Line

If you want to replace all the β€œline” with the β€œsentences” string from your file, then use the β€œ/g” flag as shown.

$ sed 's/line/sentences/g' file.txt

Output:

Replace all the occurrences of a pattern in a line

Replacing a String on a Specific Line Number

Use the following command to change the string β€œline” on the second line to the string β€œsentences”.

$ sed '2 s/line/sentences/' file.txt

Output:

Replacing the "line" string with "sentences" on the second line

Replacing All the Strings on a Specific Line Number

The following command will replace all the β€œline” strings with β€œsentences” strings in the second line.

$ sed '2 s/line/sentences/g' file.txt

Output:

Replacing all the "line" strings with "sentences" on the second line

Replacing the String on a Range of Lines

You can specify the range of lines (like the second and third line) where the string should be replaced with the specified string.

$ sed '2,3 s/line/sentences/' file.txt

Output:

Replacing the specified string on the range of lines

Replacing the Last Match Line with New Text

The following command will only replace the last line if it matches the pattern.

$ sed -e '$s/file/line/' file.txt 

Output:

Replacing the last line with the specified string

Duplicate the Replaced Line

The following command will duplicate the replaced line with a new line.

$ sed 's/line/sentences/p' file.txt

Output:

Duplicate the replaced lines with new line

Only Print the Replaced Line

Use the following command to output only the modified lines using the β€œ-n” flag.

$ sed -n 's/line/sentences/p' file.txt

Output:

Only output the replaced lines

Deleting Lines from a File

Specify the integer (referring to a line number) with the β€œd” flag to remove the specific line from the file, as shown.

$ sed 2d file.txt 

The above command will remove the second line from the file.

Delete the second line from the referenced file

Replace the β€œ2d” with β€œ$d” to delete the last line.

$ sed '$d' file.txt 

Output:

Delete the last line from the referenced file

Use the following command to specify the range of lines you need to delete.

$ sed '2,3d' file.txt 

The above command will remove all the lines between the second and third lines.

Delete the range of lines from the referenced file

To delete all the lines after a second line, use the following command.

$ sed '2,$d' file.txt 

Output:

Remove all the lines after the second line

The following command will remove the lines matching the β€œline” string.

$ sed '/line/d' file.txt 

Output:

Remove all the lines with matching strings from the referenced file

That’s the last example of the β€œsed” command.

If you have anything more to add to this article, then let us know in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.

1 thought on β€œBeginners Guide for Sed Command on Linux”