Beginners Guide for Sed Command in Linux

Linux TLDR
Last Updated:
Reading time: 4 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
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitessed
Internet RequiredNo
Discussed Tools in this Article

Sample Text File

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

$ cat file.txt 
start of file.
this is first line. this is second line. this is third line.
this is fourth line.
this is fifth line.
end of file.

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
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
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
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.
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
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 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 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 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
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
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
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
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
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
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
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
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 in Linux”