Untitled Document
sed Command

sed is a multipurpose tool which combines the work of several filters . It is derived from ed , the orignal UNIX editor . sed performs non interactive operations on a data stream - hence its name . It uses very few options but has a host of features that allow you to select lines and run instructions on them . Here is the syntax of sed command -

   sed options 'address action' file(s)

Lets see some examples of sed


1. Basic Example - Line Addressing

We have seen the head command to print the lines from a file , lets print the first three lines of emp.csv using sed command and line addressing. Below command prints first 5 lines from emp.csv . The 5 is the count for lines to be extracted and q ( quit) action to be perfomed anfter printing the lines .

   sed '5q' emp.csv    

The above example can be written in following two ways -

2. Basic Example - Line Addressing with option ( -p )

Generally , we will be using the p ( print ) command to display lines . However , this command behaves in seemingly strange manner , it outputs both the selected lines as well as all lines .So the selected lines appear twice. We must suppress this behavior with -n option and remember to use this option whenever we use the p command .

Below command prints the first five lines .

   sed -n  '1,5p' emp.csv 

3. Basic Example - Line Addressing with option ( -$p )

To print the last line of a file use $ with p . Below command prints last line of file emp.csv

   sed -n '$p' emp.csv 

4. Basic Example - Line Addressing , Selecting Lines from Anywhere

sed can select a contiguous group of lines from anywhere in the file . To select lines 9 through 15 , you have to use the follwoing command

   sed -n '9,15p' emp.csv 

5. Basic Example - Line Addressing , Selecting Multipple Group of Lines

sed in not restricted to selecting a single group of lines . You can select as many sections from just about anywhere .

   sed -n '1,5p
   $p' emp.csv 

6. Basic Example - Line Addressing ,Negating the Action ( ! )

sed also has a negation operator ( ! ) , wchih can be used with any action . For , instance selecting the first five lines is the same as not selecting lines 5 through the end . Lets run previous command (1,5p) in this way :

   sed -n '5,$p!' emp.csv 

The address and action are normally enclosed within a pair of single quotes . As you have already learned by now , you should use double quotes when variable evluation or command substitution is embeded in the quoted string .

7. Context Addressing - Print the line from emp.csv having designation as director

   sed -n '/director/p' emp.csv 

8. Context Addressing - Print the line from emp.csv having designation as director and consultant

In previous example we have print the lines having designation as director . sed command allows multiple comma separate context address . Below command find lines having designation as director and consultant .

  sed -n '/director/,/consltant/' emp.csv 

9. Writing Selected lines in file (w)- Basic Example

Irrespective of the way we select lines , we can write the selected lines to a seprate file . Save the lines of directors in dirlist in this way

  sed -n '/director/w dirlist' emp.csv 

10. Writing Selected lines in multiple files (w)-

We can store the lines pertaining to the directors, consultats and managers in three different files .

  sed -n '/director/w dirlist
          /manager/w mngrlist
          /consultant/w conlist ' emp.csv 
The -n option is required with the w command only to suppress printing of all lines on the terminal . However , even without it , selected lines will be written to the respective files.

11. Writing Selected lines in a file (w) wiith line addressing -

For a file log.txt containing 1000 lines , we can save the two halves separately .

  sed -n '1,500w log1.txt
          501,$w log2.txt' emp.csv 

12. Text Editing - Deleting Lines ( d ) -

sed used d ( delete ) command to emulate grep's -v option of selecting lines not containing the pattern . The below commands select all lines except those containing director .

  sed '/director/d' emp.csv > dlist                                         -n option  not to be used with d 
  sed -n '/director/!p' emp.csv > d list  

13. Text Editing - Deleting Blank Lines -

A blank lines consists of any number of spaces , tabs or nothing . How do you delete these lines from file ? . Here is the sed command which deletes the blank lines from a file .We need to press space and tab inside the character class .

  sed '/^[tab]*$/d' emp.csv                                       

14. Substitution ( s ) -

sed command can be used for substitution as well , lets replaced our , comma separated file emp.csv with pipe |

  sed 's/,/|/g' emp.csv     OR
  sed '1,$s/,/|/g' emp.csv                                  

15. Substitution ( s ) by specifying line addressing -

In our previous example we replace comma with pipe in entire file , now lets replace the first five lines of file emp.csv with colon :

  sed '1,5s/,/:/g' emp.csv                                       

16. Substitution - Replacing String (s) -

Lets replace the word director with member in emp.csv

  sed 's/director/member/g' emp.csv                                       

17. Substituion - Using Anchoring characters ^ and $ -

Anchoring characters are very helpful during substituion , following examples shows the use of anchoring characters -

1. Example to add 1 in front of employee id of each employee.

2. Example to suffix .00 to salary of each employee .

  1.  sed 's/^/1/g' emp.csv   
  2.  sed 's/$..00/g' emp.csv                                    

18. Multiple Substitution -

We can perform multiple substitution in single sed command , Here is the command does this task . Lets make few chnages in feedback.html file .

 $ sed 's/<I>/<EM>/g
 > s/<B>/<STRONG>/g
 > s/<U>/<EM>/g' feedback.html                                       
Untitled Document