This article explains the usage of the sed command in Linux or Unix with the help of examples. Learn to perform a number of different operations with it.
Table of Contents
Sed Command
Sed stands for Stream Editor. It is used to perform a number of operations on a given input stream or file. These operations of this command include searching, finding and replacing, insertion and deletion. Most of the time, people make use of this command to look for a particular pattern in a given input stream or to simply find a particular pattern to replace it with something else.
The benefit of using sed
is command is that you can edit the file without actually opening it up. This makes sed a very powerful and useful stream editor command. Therefore, it saves your time in the process which would otherwise be like, opening the VI Editor first, then looking up for the desired pattern to replace it.
In UNIX, this command also allows you to look for the pattern using the Regular Expressions to further perform better searching and replacing operations. The sed
command is quite efficient than other similar commands such as the ed
command because it makes only a single pass over the input(s).
Syntax
Given below is the syntax of the sed command in Linux or Unix.
1 |
sed <OPTIONS> <SCRIPT> <INPUTFILE> |
There are a number of different <OPTIONS>
available that you can check out in the sed documentation here.
Examples
Given below are the different examples illustrating the usage of the sed command in different ways for different purposes.
We’ve created a sample input text file named hello.txt
using the following bash code. We’ll use this file as the input to perform the different sed command operations.
echo 'This is a sample file. This is the Second line. Yes the Second! How are finding this tutorial? I hope you are enjoying it.' > hello.txt
Let’s just check the contents of the file using the cat
command as written below.
cat hello.txt
Example 1. Find and Replace (Substitution)
As mentioned earlier, finding a particular pattern and replacing it another pattern is a most basic and common operation for which the sed command is being used in Linux or Unix. The following sample command finds the word, Second
and replaces it with another
in our previously created file named hello.txt
.
sed 's/Second/another/' hello.txt
In the above command, s
defines the substitution
operation. The /
symbol is used as the delimiter.
This command only replaces the first occurrence of the searched pattern in each line. The other occurrences of the same pattern in any line in which one occurrence of the same pattern is already replaced remain unchanged. As you can observe in the output given below, only the first occurrence of the word Second
is replaced with another
in the line number 2 while the 2nd occurrence in the same line remains unchanged.
Note. The sed command does not actually modify the file content, rather it simply displays the modified content.
Output.
This is a sample file. This is the another line. Yes the Second! How are finding this tutorial? I hope you are enjoying it.
Example 2. Replacing nth Occurrence of a pattern in a line
The command illustrated in the first example only replaced the first occurrence of the pattern in each line. So, to replace a particular occurrence, let’s say the nth occurrence, we use the /n
flag. For example, /1
and /2
to replace the first and the second occurrence. Rest of the command remains exactly the same as above.
sed 's/Second/another/2' hello.txt
Output.
You can observe that as the flag /2
is used here so the only the second occurrence of the word Second
in line number 2 is replaced with the word another
while the first occurrence of the same remains unchanged in the same line.
This is a sample file. This is the Second line. Yes the another! How are finding this tutorial? I hope you are enjoying it.
Example 3. Replacing all the Occurrences of a pattern in a given file
So far, we’ve seen the commands to replace the first and nth occurrence of a particular pattern in each line of the file contents. This example illustrates the command using which you can replace all of the occurrences in all the lines of the file. All we need use is the global replacement flag i.e. /g
sed 's/Second/another/g' hello.txt
Output.
As you can observe in the output, both the occurrences of the word Second
are now replaced with the word another
.
This is a sample file. This is the another line. Yes the another! How are finding this tutorial? I hope you are enjoying it.
Example 4. Replacing all the occurrences, starting from nth
Occurrence till the end of the line
The letter e
appears six times in the second line of the file hello.txt
. Let’s say we do not want to replace the first and the second occurrence, rather we want to replace all of the occurrences starting from the 3rd occurrence onwards. To achieve this, we’ve to combine the flags used in Example 2 and 3. So, here we’ll make use of the /3g
flag. We’re just replacing e
with E
.
sed 's/e/E/3g' hello.txt
Output.
As you can observe only the 3rd, 4th, 5th and 6th occurrence of the letter e
is replaced.
This is a sample file. This is the Second linE. YEs thE SEcond! How are finding this tutorial? I hope you are Enjoying it.
Example 5. Regular Expression based Pattern Find & Replace
You can also make use of Regular Expressions to do the find and replace specific patterns with other patterns. The following example illustrates one such sample command that replaces the first character such that the same character is substituted in Curley braces.
sed 's/\(\b[A-Za-z]\)/\{\1\}/g' hello.txt
Output.
{T}his {i}s {a} {s}ample {f}ile. {T}his {i}s {t}he {S}econd {l}ine. {Y}es {t}he {S}econd! {H}ow {a}re {f}inding {t}his {t}utorial? {I} {h}ope {y}ou {a}re {e}njoying {i}t.
Example 6. Replacing the pattern in a Particular Line
Let’s say you want the replacement to be done only on a particular line, then you can do the same using the line number as the option itself. The following sample command replaces all of the i
characters in line 4 with capital _i_
.
sed '4 s/i/_i_/' hello.txt
Output.
It’s clear that the replacement is being done only in line number 4.
This is a sample file. This is the Second line. Yes the Second! How are finding this tutorial? I hope you are enjoy_i_ng it.
Example 7. Duplicating the Replaced Lines
This example illustrates how you can show the line with any sort of replacement done using command twice. Here we’re replacing the word are
with are YOU
in only line number 3 and making use of the /p
flag to duplicate the replaced line.
sed '3 s/are/are YOU/p' hello.txt
Output.
You can see only the line number 3 is being duplicated as it is the only line that contains replacements.
This is a sample file. This is the Second line. Yes the Second! How are YOU finding this tutorial? How are YOU finding this tutorial? I hope you are enjoying it.
Example 8. Displaying only the Replaced Lines
As you duplicate only the replaced line, similarly this command also allows you to print to the console only those lines that contain replacements. Here we’re replacing the word are
with ARE
with the -n
option to just print out the lines that contained this word and in which it is being replaced. You also need to use the /p
flag here, otherwise, this command will print nothing on the console screen, not even the replaced lines.
sed -n 's/are/ARE/p' hello.txt
Output.
As the replacement is done only in line number 3 and 4, so the first 2 lines of the file are not printed here in the console output.
How ARE finding this tutorial? I hope you ARE enjoying it.
Example 9. Replacement on a particular range of lines
Let’s say you want to replace the occurrences of a particular pattern only in a particular range of files such as from line 1 to 3. The following command does the same.
sed '1,3 s/e/E/' hello.txt
Output.
As you can see, it capitalized all of the occurrences of the letter e
from in the range of line 1 to 3.
This is a samplE file. This is thE Second line. Yes the Second! How arE finding this tutorial? I hope you are enjoying it.
You can also do the replacement process starting from a particular line number till the end of the file using the symbol $
that defines the last line of the input stream. The following command does the same replacement starting from line number 2 till the end of the file.
sed '2,$ s/e/E/' hello.txt
Output.
This is a sample file. This is thE Second line. Yes the Second! How arE finding this tutorial? I hopE you are enjoying it.
Example 10. Deleting Lines From the Input File
The sed
command can also be used to delete particular lines or lines containing the matching pattern. Here, I’ve given five different types of sample commands illustrating the different type of line deletions.
Deleting nth line
The following command will delete the 3rd line of the file.
sed '3d' hello.txt
Output.
This is a sample file. This is the Second line. Yes the Second! I hope you are enjoying it.
Deleting the last line
The following command will delete the last line of the file, hello.txt
. As earlier mentioned, the symbol $
here will represent the last line number.
sed '$d' hello.txt
Output.
This is a sample file. This is the Second line. Yes the Second! How are finding this tutorial?
Deleting all the lines lies in a particular range
This command deletes all the lines from line 1 to 3.
sed '1,3d' hello.txt
Output.
I hope you are enjoying it.
Deleting all the lines starting from nth line
It will delete all the lines starting from the 2nd line.
sed '2,$d' hello.txt
Output.
This is a sample file.
Deleting lines matching with a pattern
It will delete the line containing the word Second
.
sed '/Second/d' hello.txt
Output.
This is a sample file. How are finding this tutorial? I hope you are enjoying it.
Example 11. Inserting Blank Lines & Spaces
Inserting a blank line after each line
The following command uses the G symbol to add a single blank line after each line of the file.
sed G hello.txt
Output.
Inserting two blank lines
This command will insert two blank lines after every single line of the input file.
sed 'G;G' hello.txt
Output.
Deleting blank lines and then inserting one blank line after each line
sed '/^$/d;G' hello.txt
Inserting a blank line above each line that matches a pattern
sed '/are/{x;p;x;}' hello.txt
It will add one blank line above each line that contains the word are
.
Output.
Inserting a blank line below each line that matches a pattern
It will add one blank line above each line that contains the word sample
.
sed '/sample/G' hello.txt
Output.
Inserting a particular number of spaces to the left of each line
It inserts 10 spaces in front of each line of the input stream.
sed 's/^/ /' hello.txt
Output.
Example 12. Line Numbering
Given below are three different sample commands that prints line numbers to the input file lines in different formats.
Adding Line Numbers (Numbers on Left, Sentences Left Aligned)
Here we’re simply printing line number followed by a period, space and line content itself using the sed command.
sed = hello.txt | sed 'N;s/\n/. /'
Output.
1. This is a sample file. 2. This is the Second line. Yes the Second! 3. How are finding this tutorial? 4. I hope you are enjoying it.
Adding Line Numbers in another Format
You can print line numbers in different kind of formats. The sample command given below illustrates another format.
sed = hello.txt | sed 'N; s/^/ /;s/ *\(.\{4,\}\)\n/\1. /'
Output.
1. This is a sample file. 2. This is the Second line. Yes the Second! 3. How are finding this tutorial? 4. I hope you are enjoying it.
Adding Numbering if and only if the files contains some data or is not blank
sed '/./=' hello.txt | sed '/./N; s/\n/ /'
You can do a lot more with the sed
command. The examples illustrated above are just to show you some of the jobs that can be done using this command. Use your creativity to achieve whatsoever you want with it.
I hope you found this guide useful. If so, do share it with others who are willing to learn about the different Linux Commands and other topics that we publish here on our blog. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!