sed command 20 practical examples

sed command is a stream editor used for modifying the files in unix (or linux).  Whenever you want to make changed to the file automatically, sed comes in handy to do this. Most people never learn its power; they just simply use sed to replace text. You can do many things apart from replacing text with sed. Here i will describe the features of sed with examples. sed command 20 practical examples.

-: Sed Command Examples :-

1. Replacing or substituting string

sed command is mostly used to replace the text in a file. The below simple sed command replaces the word “Unix” with “Linux” in the file.

[root@techtutorial arkit]# cat sedexample     <<--- Original file output --
arkit is a website. arkit is located in india.
arkit learners guide.

[root@techtutorial arkit]# sed 's/arkit/Tech tutorial/' sedexample << -- After replaced with string output --
Tech tutorial is a website. arkit is located in india.
Tech tutorial learners guide.

Here the “s” specifies the substitution operation. The “/” are delimiters. The “arkit” is the search pattern and the “Tech tutorial” is the replacement string.

By default, the sed command replaces the first occurrence of a pattern in each and it won’t replace the second, third..occurrence in the line.

Note: In output (stdout) only it will show as replaced but original file value will not change using above example.

2. Replacing the Specified number occurrences of pattern in a line

Using /1, /2 etc flads to replace the first, Second occurrence of a pattern in a line. The below command replaces the second occurrence of the word “arkit” with “Tech tutorial” in a line.

[root@techtutotial arkit]# cat sedexample   <<-- Example File content output
arkit is a website. arkit is located in india.
arkit learners guide.

[root@techtutorial arkit]# sed 's/arkit/Tech tutorial/2' sedexample  <<--- After replacing search pattern output 
arkit is a website. Tech tutorial is located in india.
arkit learners guide.

3. Replacing all the Occurrences of the pattern in a line

The substitute flag /g global replacement specifies the sed command to replace all the occurrence of the string in the line.

[root@sny-nripa arkit]# sed 's/arkit/Tech tutorial/g' sedexample
Tech tutorial is a website. Tech tutorial is located in india.
Tech tutorial learners guide.

see the above example it is replaced all the matched patterns

4. Replace from nth occurrence to all occurrence in a line

Use the combination of /2, /3 with /g option to replace all patterns from nth occurrence. The following sed example will replace the second and third occurrence “arkit” word with “Tech tutorial” in a line.

[root@sny-nripa arkit]# cat sedexample  <<---- Example File output
arkit is a website. arkit is located in india. arkit
arkit learners guide.

[root@sny-nripa arkit]# sed 's/arkit/Tech tutorial/2g' sedexample <<-- After replacing pattern
arkit is a website. Tech tutorial is located in india. Tech tutorial
arkit learners guide.

5. Comment matched content line

Use the # value to comment the line for a entire matched pattern. The following example will add an comment to matched value “arkit”

[root@techtutorial arkit]# cat sedexample
arkit is a website. arkit is located in india. arkit
arkit learners guide.
arkit     <<---- Not commented --

[root@techtutorial arkit]# sed 's/^\(arkit\)$/#\1/1' sedexample
arkit is a website. arkit is located in india. arkit
arkit learners guide.
#arkit    <<----- Commented --

^Matches the null string at beginning of the pattern space, i.e. what appears after the circumflex must appear at the beginning of the pattern space.

6. Replacing the slash delimiter

In this case we would like to replace an delimiter character slash (/) when you try to replace without the escape character it will consider that “/” character as delimiter. Below is the example to replace the “/” slash.

[root@techtutorial arkit]# cat sedexample
arkit is a website. arkit is located in india. arkit
arkit learners guide.
arkit
http://arkit.co.in

[root@techtutorial arkit]# sed 's/http:\/\//www./' sedexample
arkit is a website. arkit is located in india. arkit
arkit learners guide.
arkit
www.arkit.co.in

Using too many / slash characters script will look awkward. In this we change delimiter character to different.

[root@techtutorial arkit]# sed 's_http://_www._' sedexample
arkit is a website. arkit is located in india. arkit
arkit learners guide.
arkit
www.arkit.co.in

[root@techtutorial arkit]# sed 's|http://|www.|' sedexample
arkit is a website. arkit is located in india. arkit
arkit learners guide.
arkit
www.arkit.co.in

7. Using & as the matched string

In this case we would like to replace specified pattern with special characters. In such a case & will come handy. The & presents the matched string.

Lets see the example

[root@techtutorial arkit]# sed 's/arkit/{&}/' sedexample
{arkit} is a website. arkit is located in india. arkit
{arkit} learners guide.
{arkit}
http://{arkit}.co.in

[root@techtutorial arkit]# sed 's/arkit/{&&&}/' sedexample
{arkitarkitarkit} is a website. arkit is located in india. arkit
{arkitarkitarkit} learners guide.
{arkitarkitarkit}
http://{arkitarkitarkit}.co.in

[root@techtutorial arkit]# sed 's/arkit/{&.&.&}/' sedexample
{arkit.arkit.arkit} is a website. arkit is located in india. arkit
{arkit.arkit.arkit} learners guide.
{arkit.arkit.arkit}
http://{arkit.arkit.arkit}.co.in

8. Using \1,\2 and so on up to \9

The first pair of parenthesis specified in the pattern represents the \1, the second represnts the \2 and so an. The \1,\2 can be used in the replacement string to make changes to the source string. As an example, if you want to replace the word “arkit” in a line twice as the word like “arkitarkit” use the ses command as below.

[root@techtutorial arkit]# sed 's/\(arkit\)/\1\1/' sedexample
arkitarkit is a website. arkit is located in india. arkit
arkitarkit learners guide.
arkitarkit
http://arkitarkit.co.in

if you see the above practical is replaced where “arkit” as “arkitarkit”

The parenthesis needs to be escaped with the backslash character. Another example is if you want to switch the word “arkittech” as “techarkit”, the sed command is

[root@techtutorial arkit]# cat test   <<--- Example file output
arkittech arkittech arkittech

[root@techtutorial arkit]# sed 's/\(arkit\)\(tech\)/\2\1/' test  <<--- After pattern change output
techarkit arkittech arkittech

Another example os switching the first three characters in a line

[root@techtutorial arkit]# cat test  
arkittech arkittech arkittech

[root@techtutorial arkit]# sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' test
kraittech arkittech arkittech

.Matches any character, including newline.

9. Duplicating the replaced line

The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.

[root@techtutorial arkit]# cat test   <<-- Example Text
arkittech arkittech arkittech

[root@techtutorial arkit]# sed 's/arkit/tutorial/p' test  <<-- After Print flag execution 
tutorialtech arkittech arkittech
tutorialtech arkittech arkittech

Below Example Searched string does not matched, Not printed.
[root@techtutorial arkit]# sed 's/linux/tutorial/p' test
arkittech arkittech arkittech

10. Print only replaced lines

Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.

[root@techtutorial arkit]# cat test
arkittech arkittech arkittech
this is testing

[root@techtutorial arkit]# sed -n 's/arkit/tutorial/p' test
tutorialtech arkittech arkittech

if you use -n without /p flag then sed does not print anything

11. Running multiple sed commands to accommodate the requirement

You can run multiple sed commands by pipping the output of one sed command as input to another sed command.

[root@techtutorial arkit]# sed 's/arkittech/TechTutorial/'|sed 's/testing/testingSED/' test
arkittech arkittech arkittech
this is testingSED
^C^C

Note: Do remember to provide your file name after, next to first sed command, if not your sed command will not exit until you terminate it and output will also not work for first sed command. See above example.

[root@techtutorial arkit]# sed 's/arkittech/TechTutorial/' test |sed 's/testing/testingSED/'
TechTutorial arkittech arkittech
this is testingSED

You can also use -e option without “|” pipe to run multiple sed commands in one line.  As shown in below example you know need to run sed command multiple times but you can

[root@techtutorial arkit]# sed -e 's/arkittech/TechTutorial/' -e 's/testing/testingSED/' test
TechTutorial arkittech arkittech
this is testingSED

12. Replacing string on a specified line number

You use sed command to replace the particular mentioned line number as well. See the example shown below it only replaces the string in line number 3.

[root@techtutorial arkit]# cat test
arkittech arkittech arkittech
arkittech arkittech arkittech
arkittech arkittech arkittech
this is testing

[root@techtutorial arkit]# sed '3 s/arkittech/TechTutorial/' test
arkittech arkittech arkittech
arkittech arkittech arkittech
TechTutorial arkittech arkittech
this is testing

13. Replacing string on a range of lines

You can specify a range of line numbers to the sed command for replacing a string. As shown on below example 2nd 3rd lines are replaced.

[root@techtutorial arkit]# sed '2,3 s/arkittech/TechTutorial/' test
arkittech arkittech arkittech
TechTutorial arkittech arkittech
TechTutorial arkittech arkittech
this is testing

Here is one more example with replacing the range of line

[root@techtutorial arkit]# sed '2,$ s/arkittech/TechTutorial/' test
arkittech arkittech arkittech
TechTutorial arkittech arkittech
TechTutorial arkittech arkittech
this is testing

14.  Replace on a lines which matches a pattern

Replacing a string an multiple lines for matching strings. first it will search for matched string then it will replace

[root@techtutorial arkit]# cat test
linux is opensource
arkittech arkittech arkittech
linux unix

[root@techtutorial arkit]# sed '/linux/ s/unix/TechTUTORIAL/' test
linux is opensource
arkittech arkittech arkittech
linux TechTUTORIAL

Above is the practical example which first looks for the lines which has the pattern “linux” and then replaces “unix” with “TechTUTORIAL”.

15. Deleting lines without any search strings

You can just delete the lines in a file by specifying the line numbers or a range.

[root@techtutorial arkit]# cat sedexample
1.arkit is a website. arkit is located in india. arkit
2. arkit learners guide.
3. arkit
4. http://arkit.co.in

[root@techtutorial arkit]# sed '3 d' sedexample
1.arkit is a website. arkit is located in india. arkit
2. arkit learners guide.
4. http://arkit.co.in

in above example i just deleted the line number 3 from a file.

[root@techtutorial arkit]# sed '3,$ d' sedexample
1.arkit is a website. arkit is located in india. arkit
2. arkit learners guide.

[root@techtutorial arkit]# sed '4,$ d' sedexample
1.arkit is a website. arkit is located in india. arkit
2. arkit learners guide.
3. arkit

above is the one more example which is deleting range of lines, Here “$” means up to end line number ( 4-END)

16. Duplicating line

We can make the sed command to print each line of a file two times just adding ‘p’ print flag, See the below example.

[root@techtutorial arkit]# sed 'p' sedexample
1.arkit is a website. arkit is located in india. arkit
1.arkit is a website. arkit is located in india. arkit
2. arkit learners guide.
2. arkit learners guide.
3. arkit
3. arkit
4. http://arkit.co.in
4. http://arkit.co.in

17. Print the matched pattern line using sed command

As of now we see the matched pattern is replaced with specified string but now sed command can also print the matched string lines see the below example

[root@techtutorial arkit]# cat sedexample
1.arkit is a website. arkit is located in india. arkit
2. arkit learners guide.
3. arkit
4. http://arkit.co.in

[root@techtutorial arkit]# sed -n '/http/ p' sedexample
4. http://arkit.co.in

See the above example which sed command will also replace the grep command.

18. Insert a line after the matched string

The sed command can add a new line after a pattern match is found. The “a” command to sed tells it to add a new line after the match.

[root@techtutorial arkit]# sed '/arkit/ a "INSERTING NEW LINE"' sedexample
1.arkit is a website. arkit is located in india. arkit
"INSERTING NEW LINE"
2. arkit learners guide.
"INSERTING NEW LINE"
3. arkit
"INSERTING NEW LINE"
4. http://arkit.co.in
"INSERTING NEW LINE"

if you see the above example is inserted new line after every string match.

19.  Insert a new line before string match

We can also insert a new line above of every match string by “i” command in sed.

[root@techtutorial arkit]# sed '/arkit/ i "INSERTING NEW LINE"' sedexample
"INSERTING NEW LINE"
1.arkit is a website. arkit is located in india. arkit
"INSERTING NEW LINE"
2. arkit learners guide.
"INSERTING NEW LINE"
3. arkit
"INSERTING NEW LINE"
4. http://arkit.co.in

20. Change entire line where string match

sed command can also have an capability to replace entire line where string matches, Lets see the below example.

using command “c” we can replace entire line

[root@techtutorial arkit]# sed '/learners/ c "REPLACED LINE"' sedexample
1.arkit is a website. arkit is located in india. arkit
"REPLACED LINE"
3. arkit
4. http://arkit.co.in

See the above example where matched string is replaced with entire line.

That’s for this article.. Stay tune for NEXT exiting sed command article.

Video Tutorials

Write your Feedback as comment…

Read More  Shell Scripting Training Hyderabad - Online Training Course Content

Related Articles

awk scripting basics explained

awk command part – 2

Thanks for your wonderful Support.

Stay Connected with us. Learn More and Earn More

Good Luck..!!

Ankam Ravi Kumar

Working as Linux / Storage Administrator L3. Interested in sharing the knowledge.

You may also like...

2 Responses

  1. SRINIVAS DARIPELLI says:

    Thanks Ravi. One of the best article on sed command examples.want to see more of this kind.

Leave a Reply