12 useful 'sed' commands in Linux

In this article, we will learn how to use sed command in linux with 12 practical examples. The sed command is a powerful and useful tool in Unix / Linux for editing the content (files) line by line, including inserts, appends, changes, and deletes. Furthermore, it supports regular expressions, so it can match complex patterns. Commonly it is used to find and replace the strings in files like configuration files, bash scripts, SQL files, etc.

The sed commands are mostly abstracted from the 'ed' text editor. The sed command allows us to quickly remove or replace the content without having to open a file. "For editing purposes, we have several text editors available in Linux, such as vi, emacs, vim, and jed. However, the "sed" utility will function with no limitations on any standard Unix or Linux terminal. Once you understand how syntax patterns work, it is pretty easy to use sed in Linux. This is why most experienced Linux users use the sed command since it allows them to perform powerful tasks, like substitute, insert, or delete text in a file or stream programmatically.

This guide will walk you through the most common 12 sed command in linux examples. If you are just getting started with scripting (bash), the sed utility is essential. All examples under this section have been tested on RHEL, CentOS-Stream, and Rocky Linux.

The Global Syntax for sed command in Linux is as follows (there are two forms):

sed [option]... {script} [input-file]

 

sed [option] 'command' [input-file]

Note:

- The 1st syntax executes the command from the script file.

- The 2nd syntax executes the command directly from the Terminal/Command line.

* There is not much difference between these two syntax.

An overview of the most commonly used options, flags, and special characters for Linux's sed command, is provided in the following table.

sed command in unix with options

Note:

To demonstrate all the following examples, let me first create a sample file that can be used throughout this session.

We have several options for creating files in Linux. I'll be using the 'cat' command here. This command is one of the most widely used in a standard Unix/Linux application. For more information about cat commands and options, click here

 
[[email protected] ~]$ cat > linuxteck.txt
Linux is a free and open-source operating system. Linux is a cross-platform operating system that runs on many computer models. 
Linux has attractive features and performance. Linux is a graphical user interface. 
Linux operating systems are more secure than Windows operating systems. Linux is always a multi-user operating system. ^C

TIPS:

The below substitution command is divided into five parts:

s ==> It specifies the substitution command

/ ==> It specifies the delimiters

Linux ==> It specifies the search pattern (Regular expression)

Unix ==> It specifies the replacement string.

g ==> It specifies the global replacement

1. How to search and replace all the strings/patterns without opening a file?

$ sed 's/Linux/Unix/' linuxteck.txt

 Output:

Unix is a free and open-source operating system. Linux is a cross-platform operating system that runs on many computer models.
Unix has attractive features and performance. Linux is a graphical user interface.
Unix operating systems are more secure than Windows operating systems. Linux is always a multi-user operating system.

Note:

As mentioned above, "sed" substitute command(s) is one of the most commonly used commands to replace strings or patterns in Linux. The default behavior of the sed command is to replace only the first occurrence of each string per line. Here is an example that will help us better understand. In our sample file, you can see that the word "Linux" appears twice in a line, and the changes have appeared only on the first occurrence, while the second occurrence, is not reflected. This is the default behavior of the sed command.

2. How to replace only the second occurrence on each line?

$ sed 's/Linux/Unix/2' linuxteck.txt

 Output:

Linux is a free and open-source operating system. Unix is a cross-platform operating system that runs on many computer models.
Linux has attractive features and performance. Unix is a graphical user interface.
Linux operating systems are more secure than Windows operating system. Unix is always multi-user operating systems.

Note:

The changes are reflected only in the second occurrence of each line in the above output. In this case, only the (number) "2" flag is used after the final delimiter with the same command as in the previous one. The number flag indicates the replacement of the "(n)th" occurrence of the string/pattern in the line. If you wish to change the 3rd occurrence, then replace the number flag accordingly.

3. How to restrict the replacement of a string in a particular line?

$ sed '2 s/Linux/Unix/' linuxteck.txt

 Output:

Linux is a free and open-source operating system. Linux is a cross-platform operating system that runs on many computer models.
Unix has attractive features and performance. Linux is a graphical user interface.
Linux operating systems are more secure than Windows operating systems. Linux is always a multi-user operating system

Note:

Using the above command, you will be able to restrict the replacement of strings within a particular line. As you can see in the above output, the word "Unix" was only replaced on the second line. Lastly, if you wish to replace all of the matching strings, then you can use the following "global option."

4. How to replace all occurrences of a string/pattern in a file?

$ sed 's/Linux/Unix/g' linuxteck.txt

 Output:

Unix is a free and open-source operating system. Unix is a cross-platform operating system that runs on many computer models.
Unix has attractive features and performance. Unix is a graphical user interface.
Unix operating systems are more secure than Windows operating systems. Unix is always a multi-user operating system.

Note:

In the above example output, we can see that all the matching patterns from Linux to Unix have changed. Here, we have added the global replacement flag (g) after the final delimiter along with the same substitution command as before.

5. How to print/display a series of lines from a file?

Note:

To better understand this, let us first check the total number of lines in our sample file and then follow the actual command. There are a total of six lines in the sample file.

$ sed -n p linuxteck.txt

 Output:

1. Linux is a free and open-source operating system. 
2. Linux is a cross-platform operating system that runs on many computer models.
3. Linux has attractive features and performance. 
4. Linux is a graphical user interface.
5. Linux operating systems are more secure than Windows operating systems. 
6. Linux is always a multi-user operating system.

$ sed -n '2,4p' linuxteck.txt

 Output:

2. Linux is a cross-platform operating system that runs on many computer models.
3. Linux has attractive features and performance. 
4. Linux is a graphical user interface.

Note:

We generally use the head and tail commands to print out the top and bottom of a file, but you can also use the sed command to retrieve a particular line or a series of lines from a file. Using the above command, here we have printed lines 2 to 4 from our sample file. The default behavior of the 'p' flag is to duplicate input. Using the '-n' option, you can prevent default printing.

6. How to print/display all the lines in a file except for certain portions?

Note:

By using the following command, the entire text of the file will be displayed, except for lines 2-4. Essentially, it did not remove the lines permanently from the file, just skipped over them. For a better understanding, let's print/display all the text from the demo file "linuxteck.txt" using the cat command and then use the actual command to see the difference.

$ cat linuxteck.txt

 Output:

1. Linux is a free and open-source operating system. 
2. Linux is a cross-platform operating system that runs on many computer models.
3. Linux has attractive features and performance. 
4. Linux is a graphical user interface.
5. Linux operating systems are more secure than Windows operating systems. 
6. Linux is always a multi-user operating system.

Note:

Now, let us use the actual following command to display the content, excluding the mentioned lines. The 'd' flag will temporarily exclude lines 2,3 & 4 and display the output. That doesn't mean the original file has been permanently deleted.

$ sed '2,4d' linuxteck.txt

 Output:

1. Linux is a free and open-source operating system. 
5. Linux operating systems are more secure than Windows operating systems. 
6. Linux is always a multi-user operating system.
7. How do you print/display multiple consecutive lines?

$ sed -n -e '2,3p' -e '5,6p' linuxteck.txt

 Output:

2. Linux is a cross-platform operating system that runs on many computer models.
3. Linux has attractive features and performance. 
5. Linux operating systems are more secure than Windows operating systems. 
6. Linux is always a multi-user operating system.

Note:

In the above command, we can print/display multiple consecutive lines with just a single line command. For this example, lines 2 to 3 and 5 to 6 will be printed. It is something like multiple substitutions that can be performed with the same command.

8. How do you print/display lines which contain a specific word?

$ sed -n /operating/p linuxteck.txt

 Output:

1. Linux is a free and open-source operating system. 
2. Linux is a cross-platform operating system that runs on many computer models.
5. Linux operating systems are more secure than Windows operating systems. 
6. Linux is always a multi-user operating system.

Note:

Using the above command, all the lines that use the keyword will be listed. In our example, we have used the keyword "operating" which listed four lines from a sample file.

9. How to add a line in between each line in a file?

$ sed G linuxteck.txt

 Output:

Linux is a free and open-source operating system.

Linux is a cross-platform operating system that runs on many computer models.

Linux has attractive features and performance.

Linux is a graphical user interface.

Linux operating systems are more secure than Windows operating systems.

Linux is always a multi-user operating system. ^C

Note:

In some cases, it might be nice to add space between the lines throughout the document. You can achieve the same results by choosing the 'G' option with the sed command.

10. How do you delete blank lines after each line in a file?

Note:

In some situations, it might be better to remove some unnecessary blank lines and only use the lines that contain valid text. There are several ways in Linux to eliminate blank lines or ignore them by some commands. We will use sed to accomplish just that here.

$ sed '/^$/d' linuxteck.txt

 Output:

Linux is a free and open-source operating system. 
Linux is a cross-platform operating system that runs on many computer models.
Linux has attractive features and performance. 
Linux is a graphical user interface.
Linux operating systems are more secure than Windows operating systems. 
Linux is always a multi-user operating system.

Note:

There are some situations when we may need to remove the commented and the blank rows between lines on files, similar to the above command. Taking the Apache configuration files as an example, they always begin with a lot of descriptions about the configuration file, causing the file to be too long. In that case, how can I view only the configuration file without seeing any commented or empty lines? The following command will help you remove any blank lines and any commented lines too.

$ sed '/^#\|^$\| *#/d' httpd.conf

Note:

Accessing certain files, however, requires special permission, which can be granted by doing sudo or logging in as root. As a rule of thumb, it is always recommended to use sudo privilege for all administrative commands rather than root. If you have trouble activating the sudo setup, follow these steps:

11.How to delete all lines except the range specified?

$ sed '3,5!d' linuxteck.txt

 Output:

3. Linux has attractive features and performance. 
4. Linux is a graphical user interface.
5. Linux operating systems are more secure than Windows operating systems. 

Note:

Using the above command, you can delete all the lines from a file except the range you specified. This example has eliminated every line except for line numbers 3, 4, and 5.  The "!" character, which is a negation operator, is used to keep a specified range of lines.

12. How to ignore the case of replacing the patterns in a file?

Note:

The sed command is case-sensitive by default, so if the search word appears in a mixed case and you try to replace it, then the "sed" would not take all occurrences into account. To achieve this, we must use the "-i" flag at the end of the delimiter to avoid case sensitivity. To gain a better understanding, let us first view the file content.

$ cat linuxteck.txt

 Output:

Linux is a free and open-source operating system. Linux is a cross-platform operating system that runs on many computer models.
linux has attractive features and performance. Linux is a graphical user interface.
Linux operating systems are more secure than Windows operating systems. Linux is always a multi-user operating system. ^C

Note:

We can easily spot the 2nd line of the word "linux" has been lower cased. Due to the default behavior of sed, we can only substitute case-sensitively.

$ sed 's/linux/unix/' linuxteck.txt

 Output:

Linux is a free and open-source operating system. Linux is a cross-platform operating system that runs on many computer models.
unix has attractive features and performance. Linux is a graphical user interface.
Linux operating systems are more secure than Windows operating systems. Linux is always a multi-user operating system. ^C

Note:

As you can see, only one occurrence was updated. Because we only included the lower case search phrase "linux" in the command section, sed is only able to identify and replace matching words. By using the "-i" flag, sed ignores case-sensitive input, so it will replace words in either uppercase or lowercase.

 $ sed 's/linux/unix/i' linuxteck.txt

 Output:

unix is a free and open-source operating system. Linux is a cross-platform operating system that runs on many computer models.
unix has attractive features and performance. Linux is a graphical user interface.
unix operating systems are more secure than Windows operating systems. Linux is always a multi-user operating system. ^C

Note:

The "-i" flag clearly ignores the case of words and only replaces the first occurrence of the targeted word. If you wish to replace the second and all occurrences, please follow steps 2,3 and 4.

Thank you for taking the time to read! I hope this article will help you to understand the 12 useful sed commands in Linux. Drop me your feedback/comments. If you like this article, kindly share it and it may help others as well.

Thank you!

About John Gomez

John Britto Founder & Cheif-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 10+ years of experience in Linux and Open Source technologies.

View all posts by John Gomez →

1 reply on “12 useful 'sed' commands in Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *

L