The “sed” Command in Linux [7 Practical Examples]

The sed command is used to work in a stream editor called Sed in Linux. Sed editor can make basic text transformations in a file. With the help of the sed command, you can edit text files without opening files. In this article, you will learn the ins and outs of the sed command in Linux.

A. Description

The sed command is a very useful command in Linux. Using this command, you can edit files without even opening them. You can replace the words in the files with the help of the sed command in Linux. You can also delete and add lines of the files using this command.

B. Syntax

The sed command has a very simple syntax in Linux. And the syntax is as follows:

sed [OPTION]... [COMMAND] [FILE]

Note: In the syntax above, OPTION is enclosed by a square bracket and followed by 3 dots representing that more than one option can be used at the same time and COMMAND is a specific instruction to transform a text in a text file. Moreover, FILE suggests that you can specify one file here.

C. Options

A few options are available for the sed command. I have mentioned the most used options of the command here. However, you can look into the man page for the sed command to know more about its options.

man sed

Useful Options

  • -i, Modifies and saves the original file.
  • -v, Displays version information, and exit.

Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be cautious when you use them.

Practical Examples of the “sed” Command in Linux

The sed command in Linux is a pretty straightforward command which is used to edit files without opening them. Here you will learn some useful examples of the sed command. In this article, I have worked with the files below.

Download this file to work with the “sed” command in Linux

Example 1: Replace the First Occurrence of a Word in a File

Using the sed command you can replace the first occurrence of a word in a file in Linux. Here I want to replace the word two with three in the file1.txt using the sed command in Linux. To do the same you can follow the procedure below:

Steps to Follow:

➊ At first, launch the Ubuntu Terminal.

➋ Run the command below in the command prompt to view the file1.txt:

cat file1.txt

➌ Now, tap the ENTER button.Replace the First Occurrence of a Word in a File❹ To replace the word, copy the following command in the command prompt:

sed 's/two/three/' file1.txt

➎ Then, hit the ENTER key.

Output:

As you can see, the word two is replaced with the word three in the file1.txt in the output.Replace the First Occurrence of a Word in a File Using the “sed” Command in Linux


Similar Readings


Example 2: Replace All the Occurrences of a Word in a File Using the “sed” Command in Linux

You can replace all the occurrences of a word in a file using the sed command in Linux. Here I want to replace the word season with seasons in the file1.txt using the sed command. You can also do the same by following the process below:

Steps to Follow:

➊ Initially open the Ubuntu Terminal.

➋ To view the file1.txt, type the following command in the command prompt:

cat file1.txt

➌ Now, tap the ENTER button.Replace the First Occurrence of a Word in a File❹ To replace the word, write the following command in the command prompt:

sed 's/season/seasons/g' file1.txt

➎ Then, hit the ENTER key.

Output:

As you can see in the image below, all the occurrences of the word season are replaced with the word seasons in the file1.txt in the output.Replace the First Occurrence of a Word in a File

Example 3: Replace a Word in the Specific Line Number of the File Using the “sed” Command in Linux

You can also replace a word in the specific line number of the file using the sed command in Linux. Here I want to replace the word months with weeks in line no 3 of the file1.txt using the sed command in Linux. You can also do this by following the steps below:

Steps to Follow:

➊ Start by opening the Ubuntu Terminal.

➋ Execute the following command in the command prompt to view the file1.txt:

cat file1.txt

➌ Then hit the ENTER button.Replace a Word in the Specific Line Number of the File❹ To replace the word, write the following command in the command prompt:

sed '3s/months/weeks/' file1.txt

➎ After that, tap the ENTER key.

Output:

In the image below, the word months is replaced with the word weeks in line no 3 of the file1.txt in the output.Replace a Word in the Specific Line Number of the File Using the “sed” Command in Linux


Similar Readings


Example 4: Display Only Specific Lines of the File

You can display specific lines of the file using the sed command in Linux. Here I want to display only lines 4 to 6 of the file1.txt using the sed command. To display specific lines of the file follow the instructions below:

Steps to Follow:

➊ To initiate, launch the Terminal in Ubuntu.

➋ Type the command below in the command prompt to view the file1.txt:

cat file1.txt

➌ Then hit the ENTER button.Display Only Specific Lines of the File Using the “sed” Command in Linux❹ Write the following command in the command prompt to view the file1.txt:

sed -n '4,6p' file1.txt

➎ After that, tap the ENTER key.

Output:

In the following image, you can see only lines 4 to 6 of the file1.txt are displayed in the output.Display Only Specific Lines of the File

Example 5: Delete a Specific Line of the File Using the “sed” Command in Linux

To delete a specific line of the file you can use the sed command in Linux. Here I want to delete line no 6 of the file1.txt. You can also do this by following the following instructions:

Steps to Follow:

➊ Firstly open the Ubuntu Terminal.

➋ Execute the following command in the command prompt to view the file1.txt:

cat file1.txt

➌ Then hit the ENTER button.Delete a Specific Line of the File❹ To delete a line, copy the following command in the command prompt:

sed '6d' file1.txt

➎ Finally, press the ENTER key.

Output:

In the image below, the 6th line of file1.txt does not exist in the output.Delete a Specific Line of the File


Similar Readings


Example 6: Add Line After Specific Line Number

To add a line after a specific line number you can use the sed command in Linux. Here I am adding a line after line no 13 of file1.txt. To add a line after a specific line number of the file follow the procedure below:

Steps to Follow:

➊ Launch the Ubuntu Terminal.

➋ Type the following command in the command prompt to view the file1.txt:

cat file1.txt

➌ After that, press the ENTER button.Add Line After Specific Line Number Using the “sed” Command in Linux❹ To add a line, copy the following command in the command prompt:

sed '13a\Among all the seasons, I love Spring.' file1.txt

➎ Finally, hit the ENTER key.

Output:

In the following image, a line is added after the 13th line of the file1.txt in the output.Add Line After Specific Line Number Using the “sed” Command in Linux

Example 7: Delete the Lines Containing Matched Patterns in a File

You can also delete the lines containing matched patterns using the sed command in Linux. Here I want to delete the lines of the file1.txt containing the pattern season. You can also do this by following the following process:

Steps to Follow:

➊ To start, launch the Ubuntu Terminal.

➋ Run the following command in the command prompt to view the file1.txt:

cat file1.txt

➌ Now, tap the ENTER button.Delete the Lines Containing Matched Patterns in a File❹ To delete lines, copy the following command in the command prompt:

sed '/season/d' file1.txt

➎ Finally, strike the ENTER button.

Output:

In the following image, the lines of file1.txt containing the pattern season are deleted in the output.Delete the Lines Containing Matched Patterns in a File

Conclusion

As seen in this article, the sed command has some significant uses in Linux. You’ve also found the syntax, some functional options, and the practical applications of this command. To become an expert in Linux, practice the command and its practical applications.


Similar Readings

Rate this post
Jannatul Ferdousi Sylvie

Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

We will be happy to hear your thoughts

Leave a reply

LinuxSimply
Logo