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

The sed command in Linux stands for stream editor which allows for editing text streams in a file. Though primarily used for text substitution, the “sed” command can perform different operations on file such as insertion, deletion, find and replace, and searching using the stream editor called “sed”. The “sed” command can even edit files without opening them. On top of that, its ability for pattern matching using regular expression makes it a powerful text manipulation tool.

In this tutorial, you’ll learn how to use the sed command in Linux to edit and manipulate files quickly and efficiently by grasping the necessary concepts with 7 practical examples.

The Linux “sed” Command Syntax

The sed command has a very simple syntax in Linux. 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.

The Linux “sed” Command 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

Option

Description

-i Modifies and saves the original file.
-v Displays version information, and exits.
Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be cautious when you use them.

7 Practical Examples of the “sed” Command in Linux

Here are some examples of sed commands listed below:

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 open the terminal and follow the procedure below:

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

    Replace the First Occurrence of a Word in a File

  2. To replace the word, copy the following command in the command prompt and hit the ENTER key:
    sed 's/two/three/' file1.txt

    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

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:

  1. View the file1.txt by running the following command in the prompt:
    cat file1.txt

    Replace the First Occurrence of a Word in a File

  2. To replace the word, run the following command in the command prompt:
    sed 's/season/seasons/g' file1.txt

    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

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. To view the original file, run the following command:

cat file1.txt

Replace a Word in the Specific Line Number of the File To replace the word months with weeks, run the following command in the terminal:

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

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 below guide:

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

    Display Only Specific Lines of the File Using the “sed” Command in Linux

  2. Write the following command in the command prompt to view the line 4 to 6 of file1.txt:
    sed -n '4,6p' file1.txt

    So, 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

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 steps:

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

    Delete a Specific Line of the File

  2. To delete a line, copy the following command in the command prompt and press ENTER:
    sed '6d' file1.txt

    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 do the same, run the command mentioned below:

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

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 step-by-step process:

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

    Delete the Lines Containing Matched Patterns in a File

  2. To delete lines, run the below command:
    sed '/season/d' file1.txt

    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 7 practical applications of this command. To become an expert in Linux, practice the command and its practical applications.

People Also Ask

What is the sed command in Linux?

The sed command allows users to edit files and streams on Linux using the sed stream editor. It’s a powerful tool that can search, replace, add, or delete lines in a text file even without opening them. The sed command also supports regular expressions allowing for complex pattern matching.

How to replace text in a file using the sed command?

To replace text in a file using the sed command, use the syntax sed ‘s/old_string/new_string/’ filename.txt. This will replace the old_string with the new_string of filename.txt. For instance, to replace the word “universe” with “multiverse” in a file called “marvel.txt”, run the command sed 's/universe/multiverse/' marvel.txt in the terminal.

Does the sed command edit the source file directly?

No. The sed command does not edit the source file directly unless instructed. Rather the command displays only the changes while the original file remains the same. However, you can use the -i option with the sed command to overwrite the source file directly. Alternatively, you can save the modification in a different file by using output redirection.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
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

Leave a Comment