How to use sed tool

Table of Contents

Introduction

Stream editor or Sed tool helps with the text-based operation on both files and the terminal outputs. For example,

– We can find or replace specific words directly on the text file. 

– We can also display a section from a text file and modify it 

In this article we will be covering the operations such as insertion , deletion and substitution supported by the sed tool in detail with examples 

We will be using the following example file to demonstrate the sed use cases 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

Using Sed to print a section from the text file

In this example we are printing the first 3 lines of the text file. 

-n to  suppress automatic printing of pattern space

‘1,3p’ to print line 1-3 ( p for print , used to display lines as per the [startline , end line notation] )

[root@discoveringsystems-centos ~]# sed -n '1,3p' discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is powerful when used along with regex
this file is named as discovering-systems.txt

Or ( Both produces the same result , as sed also works with the terminal output when piped ) 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt |  sed -n '1,3p'
this is a example text to demo the use cases of the sed
sed is powerful when used along with regex
this file is named as discovering-systems.txt

Using Sed to print rest of the lines without the section of lines specified 

In this example we are printing all other lines from the text , except the specified lines 

‘1,3d’ to print all other lines except the lines 1-3 ( d is to delete the lines specified in [startline,endline] notation and print the rest  )

[root@discoveringsystems-centos ~]# sed  '1,3d' discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

Or ( Both produces the same result , as sed also works with the terminal output when piped ) 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt | sed  '1,3d'
this is the fourth line
here is the fifth line
finally 6th line

Using Sed to Print non-consecutive lines 

In this example we will be printing non consecutive lines 

-n  to  suppress automatic printing of pattern space

-e to execute the action  ‘1,2p’ , ‘5,6p’ , etc multiple times 

‘1,2p’ print lines 1 and 2 

‘5,6p’ print lines 5 and 6 

[root@discoveringsystems-centos ~]# sed -n -e '1,2p' -e '5,6p' discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is powerful when used along with regex
here is the fifth line
finally 6th line

Using sed to print lines with a space 

Here we are going to print the same original text file contents , but using sed we will be adding some empty line spaces.

G – to add a empty line between the lines of the text 

[root@discoveringsystems-centos ~]# sed G discovering-systems.txt
this is a example text to demo the use cases of the sed

sed is powerful when used along with regex

this file is named as discovering-systems.txt

this is the fourth line

here is the fifth line

finally 6th line

If we would like to add multiple empty lines in the output we can use the ‘G:G’ 

[root@discoveringsystems-centos ~]# sed 'G;G' discovering-systems.txt
this is a example text to demo the use cases of the sed


sed is powerful when used along with regex


this file is named as discovering-systems.txt


this is the fourth line


here is the fifth line


finally 6th line

Using sed to print text with substitution 

Using sed we can substitute one word in place of another while printing the output 

Syntax : sed s/actual-text/substitution-text/g filename 

S means substitution 

G to replace all occurrences of the matching pattern

with g we can also pass the number to denote which occurrence we would like to substitute 

With gi we can substitute the replacement word with case insensitivity 

# the original unmodified file 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line
#”Finally” word has been replaced with “eventually” 

[root@discoveringsystems-centos ~]# sed s/finally/eventually/g discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
eventually 6th line

#all occurrences of ”this” is replaced with “that” 

[root@discoveringsystems-centos ~]# sed s/this/that/g discovering-systems.txt
that is a example text to demo the use cases of the sed
sed is powerful when used along with regex
that file is named as discovering-systems.txt
that is the fourth line
here is the fifth line
finally 6th line
#only substitute ‘this’ with ‘that’ in the line range from 2 through 4 

[root@discoveringsystems-centos ~]# sed '2,4s/this/that/g' discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is powerful when used along with regex
that file is named as discovering-systems.txt
that is the fourth line
here is the fifth line
finally 6th line

Using sed to handle case sensitivity

Here in this example we will replace the word “Powerful” with ”incredible”  using sed by handling the case sensitivity 

#original text 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is Powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line
# This was not working due to case sensitivity 

[root@discoveringsystems-centos ~]# sed 's/powerful/incredible/g' discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is Powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line
# Making it case insensitive with  “i” along with “g” 

[root@discoveringsystems-centos ~]# sed 's/powerful/incredible/gi' discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is incredible when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

Using regex in sed modifications

We are going to perform replacing the word “Powerful” with “incredible” using regular expressions instead of using the case insensitivity option.  In this example  regular expression “[Pp]owerful” matches both “powerful” and “Powerful” to deal with the case sensitivity of the word “powerful” in the original file 

#original text 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is Powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line
 
# using regex with sed 

[root@discoveringsystems-centos ~]# sed 's/[Pp]owerful/incredible/g' discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is incredible when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

Using sed to  perform multiple substitution

Here in this example we are modifying the the word “this” with “that” and at the same time we are also replacing the word “powerful” with “incredible” by passing two substitution rules with semicolon in the already discussed config syntax 

# the original unmodified file 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line
#Sed with multiple modification rules in a single command 

[root@discoveringsystems-centos ~]# sed 's/this/that/g;s/powerful/incredible/g' discovering-systems.txt
that is a example text to demo the use cases of the sed
sed is incredible when used along with regex
that file is named as discovering-systems.txt
that is the fourth line
here is the fifth line
finally 6th line

Using sed to replace only if match is found

We can replace a word when a match is found . Here in this example we can see that i want to replace the word “this” with “that” only when the line has ‘txt’ in it 

#original text 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

#result after sed substitution 

[root@discoveringsystems-centos ~]# sed '/txt/ s/this/that/g' discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is powerful when used along with regex
that file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

Here In this example , we are replacing the word , only if the “txt” match pattern is not found on a line , technically the reverse of what we were doing in the earlier example 

[root@discoveringsystems-centos ~]# sed '/txt/!  s/this/that/g' discovering-systems.txt
that is a example text to demo the use cases of the sed
sed is Powerful when used along with regex
this file is named as discovering-systems.txt
that is the fourth line
here is the fifth line
finally 6th line

Using sed to add new word before and after the matched word 

The following example , adds the word “the” before the word sed and the word “tool” after the word sed based on the match 

#original text 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is Powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line


# after modification using the sed 

[root@discoveringsystems-centos ~]# sed 's/\(sed\)/the \1 tool /' discovering-systems.txt
this is a example text to demo the use cases of the the sed tool
the sed tool  is Powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

Sed examples to perform modification without opening the file through other file editors

In the already discussed examples we were not modifying the original file but only the printed output while reading the text file. Sed can also make the changes directly on the file instead of only producing variation in the displayed output 

We are going to edit the following text file directly using sed  , but it’s best to use a backup option to keep original file safe 

#original text 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is Powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

In this example ,  if you specify -i option without a string , then we won’t create the backup of the original file , but we will just edit the original file  , but here to save the original file safely i am using the backup 

[root@discoveringsystems-centos ~]# sed -i'.backup' 's/[Pp]owerful/incredible/g' discovering-systems.txt

# viewing the modified file 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the sed
sed is incredible when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

# viewing the backup unmodified  file 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt.backup
this is a example text to demo the use cases of the sed
sed is Powerful when used along with regex
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

Leave a Comment

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