Understanding sed command with example -Part 2

sed is a *nix stream editor command with the ability to filter and transform input text files line by line. sed command works very well with awk command and they are used together by System Administrators to simplify day to day activities. The syntax for sed command is
sed [options] commands <input-file>
In the earlier post we saw how to print, quit and delete a particular line with sed command. In this second of the two post series on sed we will cover 3 more widely used commands. They are 
  1. Substitute (s/../../) command
  2. Change (c \) command
  3. Insert (i \) command 
Before going into the details lets create a file name example.txt. The rest of this tutorial will be explained based on this file. The content of the file is given below
cat example.txt
Sample Output:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
 

Substitute (s/.../.../) command

Substitute command will search a pattern and replace the occurrence of that pattern with a new value. This will work similar to Find and Replace tool available in any graphical text editor. The syntax for substitute command is 
sed [options] 's/search_pattern/replacement_pattern/' <input-file>
where s is the substitute command, /../../ is the delimiter, search_pattern and replacement pattern are the respective search and replacement values provided by the user.Now lets see few examples with substitute command using example.txt file

#1)  To substitute the word "line" with "row "in example.txt, execute
sed 's/line/row/' example.txt
This is row 1
This is row 2
This is row 3
This is row 4
This is row 5
This is row 6

#2)  Substitute command is a powerful tool of sed command and should be handled carefully. I'll explain why, with a simple example. Suppose in the example.txt file you need to replace the second word "is" with the word "was" and in the command line you executed
sed 's/is/was/' example.txt
the Output will be:
Thwas is line 1
Thwas is line 2
Thwas is line 3
Thwas is line 4
Thwas is line 5
Thwas is line 6
Here you can see instead of the word "is", sed replaced the pattern "is" in the word "This". And if you are wondering how to substitute the word "is", simply execute
sed 's/ is / was /' example.txt
or
sed 's/[[:space:]]is[[:space:]]/[[:space:]]was[[:space:]]/' example.txt
which means there is a space before and after the string "is" and now the output will be:
This was line 1
This was line 2
This was line 3
This was line 4
This was line 5
This was line 6

#3)  In this example I am trying to put the pattern "is" inside a simple bracket
sed 's/is/(is)/' example.txt
Sample Output:
Th(is) is line 1
Th(is) is line 2
Th(is) is line 3
Th(is) is line 4
Th(is) is line 5
Th(is) is line 6

Even though the pattern "is" appears twice in each line only the first occurrence is substituted. In order to make changes to every occurrence of the search pattern we use the sed flag '/g' or the global replacement flag. The global replacement flag is added at the end of last delimiter. So inorder to put every occurrence of the pattern "is" inside a simple bracket, execute
sed 's/is/(is)/g' example.txt
Sample Output:
Th(is) (is) line 1
Th(is) (is) line 2
Th(is) (is) line 3
Th(is) (is) line 4
Th(is) (is) line 5
Th(is) (is) line 6

#4) To substitute the patterns in the third line alone, execute

sed '3 s/line/row/' example.txt
Sample Output:
This is line 1
This is line 2
This is row 3
This is line 4
This is line 5
This is line 6

#5) To substitute the patterns from line number 3 to line number 5, execute
sed '3,5 s/line/row/' example.txt
Sample Output:
This is line 1
This is line 2
This is row 3
This is row 4
This is row 5
This is line 6

#6) To substitute the patterns in a line containing the string "line 2" alone, execute
sed '/line 2/ s/line/row/' example.txt
Sample Output:
This is line 1
This is row 2
This is line 3
This is line 4
This is line 5
This is line 6

#7) To substitute the patterns in a range of lines starting from the line containing string "line 4" to the line containing the string "line 6", execute
sed '/line 4/,/line 6/ s/line/row/' example.txt
Sample Output:
This is line 1
This is line 2
This is line 3
This is row 4
This is row 5
This is row 6

#8) To substitute the patterns except third line , execute
sed '3! s/line/row/' example.txt
Sample Output:
This is row 1
This is row 2
This is line 3
This is row 4
This is row 5
This is row 6

#9) To exclude substitution in a range of lines, starting with the line containing the string "line 3" and ending at line containing the string "line 5", execute
sed '/line 3/,/line 5/! s/line/row/' example.txt
Sample Output:
This is row 1
This is row 2
This is line 3
This is line 4
This is line 5
This is row 6

Change (c\) command

If Substitute command was used to replace a pattern, we use Change (c\) command to replace an entire line with a new one

#1) To replace the third line, execute
sed '3 c\This is third line' example.txt
Sample Output:
This is line 1
This is line 2
This is third line
This is line 4
This is line 5
This is line 6
 

#2) To replace the line containing the string "line 6", execute
sed '/line 6/ c\This is the last line' example.txt
Sample Output:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is the last line

#3) To replace all lines except line number 3, execute
sed '3! c\Hello World' example.txt
Sample Output:
Hello World
Hello World
This is line 3
Hello World
Hello World
Hello World

Insert (i\) command

Insert command inserts a new line above a search pattern or the line number
#1) To insert a new line above the first line
sed '1 i\Hello World' example.txt
Sample Output:
Hello World This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6

#2) To insert a new line above the line containing the string "line 4"
sed '/line 4/ i\--------------' example.txt
Sample Output:
This is line 1
This is line 2
This is line 3
--------------
This is line 4
This is line 5
This is line 6

For more details on sed refer the man pages.

Comments

Popular posts from this blog

Understanding awk command with examples

Understanding sed command with example -Part 1

How to install a Software in Linux