Lets learn SED tool a very intelligent streaming editor in Linux system.

  • Sed  is a stream editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). 
  • While in some ways similar to an editor which permits scripted  edits  (such as  ed),  sed works by making only one pass over the input(s), and is consequently more efficient. 
  • But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

The best way to learn SED tool is to use several examples:

1. Suppose we have a file containing below lines in it:

Prompt:> cat testFile
 1.This is s test file
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for
5.reading and processing
6.This file can be deleted later when

8.
9.no longer needed
10.Thanks

***************************Deleting*************************

Now I want to delete 5th line from the file and print rest file:

sed '5d' testFile

The output is as below:

shanky@localhost:/home/shanky/test:> sed '5d' testFile
1.This is s test file
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for
6.This file can be deleted later when

8.
9.no longer needed
10.Thanks

Note:- SED is just a filter. It does not affect the original file.


2. Delete 3rd line and every 2nd line from 3rd line onward.

sed '3~2d' testFile

shanky@localhost:/home/shanky/test:> sed '3~2d' testFile
1.This is s test file
2.for awk ans sed assignement
4.It contains some demo lines for
6.This file can be deleted later when
8.
10.Thanks


3. Now supppose, I want to delete lines from 3 to 7 of the file. How do we do it?

sed '3,7d' testFile

shanky@localhost:/home/shanky/test:> sed  '2,6d' testFile
1.This is s test file

8.
9.no longer needed
10.Thanks

4. To delete the last line of the file.

sed '$d' testFile

shanky@localhost:/home/shanky/test:> sed '$d' testFile
1.This is s test file
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for
5.reading and processing
6.This file can be deleted later when

8.
9.no longer needed

Here, $ refers to the last line of the file.

5. To delete all blank lines from the file.

sed '/^$/d' testFile

shanky@localhost:/home/shanky/test:> sed '/^$/d' testFile
1.This is s test file
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for
5.reading and processing
6.This file can be deleted later when
8.
9.no longer needed
10.Thanks

7th line is a blank line.


***********************Printing*************************

1. To print any particular line n of a file

sed -n '5p' testFile

The above command will print only 5th line of the file on the console.

shanky@localhost:/home/shanky/test:> sed -n '5p' testFile
5.reading and processing

 

2. To print from a file in a range n1 to n2.

sed -n '5,10p' testFile

shanky@localhost:/home/shanky/test:> sed -n '5,8p' testFile
5.reading and processing
6.This file can be deleted later when

8.


************Search using pattern*****************

1. Search a pattern and print from a matching line to the end of file

shanky@localhost:/home/shanky/test:> sed -n '/awk/,$p' testFile
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for
5.reading and processing
6.This file can be deleted later when

8.
9.no longer needed
10.Thanks

2.Search a pattern and print from a matching line and two more lines

shanky@localhost:/home/shanky/test:> sed -n '/awk/,+2p' testFile
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for

 

3.Search and print lines matchingfrom first patteern to second pattern

shanky@localhost:/home/shanky/test:> sed -n '/awk/, /be/p' testFile
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for
5.reading and processing
6.This file can be deleted later when

 


***********************Substitution**********************

To substitute a word in the file by another word. One or more occurances of a word can be replaced by another words in a line or in a range of file. This can be done by substitute command "s"

Example 1:  

sed -e 's/find/replace/' inputFile

Above command will replace every occurance of "find" string by the "replace" string throughout the file "inputFile".

shanky@localhost:/home/shanky/test:> sed -e 's/file/mile/' testFile
1.This is s test mile
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for
5.reading and processing
6.This mile can be deleted later when

8.
9.no longer needed
10.Thanks

If you want you can put all the rules in a file and use it upon the target file in one go using -f option. For eg. I put substition rule in sedfile.sed and aplly it on the target file.

sed -f sedIPfile -i.timestamp targetfile

See the content of sedIPfile

#! /usr/bin/sed -f
s/find-word/replace-word/

So the above command will first take a backup of the target file targetfile with the current timestamp then it will make changes
in it as per the rules given in sed input file sedIPfile.

 

Note:- Here -i option helps to take back up, before making any changes.

 -i[SUFFIX], edit files in place (makes backup if extension supplied)

Example 2:

sed -e '2s/find/replace/' inputFile

The above command will replace occurance of "find" string by the "replace" string only in the line no. 2 of the file "inputFile".

shanky@localhost:/home/shanky/test:> sed -e '6s/file/mile/' testFile
1.This is s test file
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for
5.reading and processing
6.This mile can be deleted later when

8.
9.no longer needed
10.Thanks

Example 3:

sed -e '2,10s/find/replace/' inputFile

The above command will replace occurance of "find" string by the "replace" string only from line no. 2 to line no. 10 of the file "inputFile".

shanky@localhost:/home/shanky/test:> sed -e '1,3s/file/mile/' testFile
1.This is s test mile
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for
5.reading and processing
6.This file can be deleted later when

8.
9.no longer needed
10.Thanks


Example 4:

sed -e '/first/,/last/ s/find/replace/' inputFile

This command is a little bit diferent. Here the range is specified as strings. The above command will do a find-replace in the file starting from first occurance of string "first" and end when the string "last" is found.

  • When ever the word "first" is found the search is turned on and when the word "last" is matched it stops the substitution.
  • Also, the subtitution happens between every pair of the first and last word if the patters appear many times in the file

shanky@localhost:/home/shanky/test:> sed -e '/file/,/when/ s/sed/dead/' testFile
1.This is s test file
2.for awk ans dead assignement
3.purpose
4.It contains some demo lines for
5.reading and processing
6.This file can be deleted later when

8.
9.no longer needed
10.Thanks

Example 5:

sed -e '2,/last/ s/find/replace/' inputFile

The above command is same as the previous command but it works from line no.2 to the first occurance of string "last"

Additionally it also replaces the first occurance of "find" word in every line containing the word "last"

shanky@localhost:/home/shanky/test:> sed -e '3,/when/ s/sed/dead/' testFile
1.This is s test file
2.for awk ans sed assignement
3.purpose
4.It contains some demo lines for
5.reading and processing
6.This file can be deleted later when

8.
9.no longer needed
10.Thanks


 Example 6 :

Remove trailing '.' from each line:

The below command will substitute a dot by nothing provided the dot should be at the end of the line.

$sed 's/.$//' filename


Example 7 :
Revert the file content

The below command will put the content of first line to the last line and vice versa.

$ sed -n '1!G;h;$p' shankys.txt

G g : It copies the content from hold space to pattern space

h H : It copies the content from pattern space to hold space

1 gives the start position and '$' denotes to the last position.

So, The above command will start scanning from first line till the last line and swap the content from first line with last line.


Example 8

Add a serial number(1,2,3..) at the begining of each line.

$ sed '/./=' shankys.txt | sed 'N; s/\n/ /'

The above command will first add a serial number like 1,2,3,..for each line and then will put the number and each line content in the same line. See the output:

shankys@local:> sed '/./=' test
1
first
2
second
3
third

shankys@local:>sed '/./=' test|sed 'N;s/\n/ /'
1 first
2 second
3 third