The Stream Editor sed



  1. What is sed?
    sed is the stream editor. sed copies the standard input to standard output, edited according to a script of commands. It is extremely useful for manipulating strings or variables in shell scripts. The command script is applied to each line of the input.

  2. Quick example of sed.
    If we wanted just the list of users that were logged on we could enter:
    $ who | sed 's/ .*$//'
    operator
    operator
    kochis
    wang
    king
    nobles
    gary
    www
    $
    


  3. sed editing commands.
    The general format of an editing command is:
    [address1,address2][function][arguments]
    One or both of the addresses may be omitted. Any number of blanks or tabs may separate the addresses from the function. The function must be present. arguments may or may not be required depeneding on the function. sed commands are terminated by the end of line.

  4. Options for the sed command.
    Two useful options when using sed are:
    -n
    tells sed not to copy all lines to output. Only those lines specified with p functions or p flags are copied.
    -f
    tells sed to take its commands from a file. One command per line.


  5. Test file
    All of the sed examples willl use this data file. The fields are separated by a tab.
    able	old	100
    able	new	200
    able	blue	200
    baker	old	300
    baker	new	300
    baker	blue	200
    charlie	old	100
    charlie	new	400
    charlie	blue	400
    dog 	old	230
    dog 	new	410
    dog	blue	700
    


  6. Addresses.
    Addresses may be either line addresses or context addresses.
    1. Line number addresses
      A line address is a decimal integer. As each line is read from the input, a line number counter is icremented. A line address selects a line when the line number is equal to line number counter. The counter runs cumulatively through multiple input files; it is not reset when a new input file is opened. As a special case, the character $ matches the last input line.
    2. Context Addresses
      A context address is a regular expression enclosed in slashes('/'). Each line that matches the regular expression is operated on.
    3. Number of addresses.
      A command may have 0,1,or 2 addresses. If a command has no addresses, it is applied to every line. One address commands are applied to all lines that match that address. Two address commands are applied to the first line that matches which matches the first address, and to all subsequent lines until (and including) the next line to match the second address. Two addresses are separated by a comma. Two addresses form a range.
    4. Examples
      1. No address, remove the first word from every line.
        $ sed 's/^[a-z][a-z]*.//' sedfile
        old     100
        new     200
        blue    200
        old     300
        new     300
        blue    200
        old     100
        new     400
        blue    400
        old     230
        new     400
        blue    700
        

      2. One line number. Print the 5th line.
        $ sed -n '5p' sedfile
        baker   new     300
        

      3. One context address, print all the blue lines.
        $ sed -n '/blue/p' sedfile
        able    blue    200
        baker   blue    200
        charlie blue    400
        dog     blue    700
        

      4. Two line numbers, Remove lines 5,8 from the output.
        $ sed '5,8d' sedfile
        able    old     100
        able    new     200
        able    blue    200
        baker   old     300
        charlie blue    400
        dog     old     230
        dog     new     410
        dog     blue    700
        

      5. Two context addresses. Print the lines between 400 and 230.
        $ sed -n '/400/,/230/p' sedfile
        charlie new     400
        charlie blue    400
        dog     old     230
        


  7. Line Functions
    1. d delete
      lines from the file. All matched lines are not written to the output.
      $ sed '1,4d' sedfile
      baker   new     300
      baker   blue    200
      charlie old     100
      charlie new     400
      charlie blue    400
      dog     old     230
      dog     new     410
      dog     blue    700
      
      $ sed '/blue/d' sedfile        
      able    old 	100
      able    new 	200
      baker   old 	300
      baker   new     300
      charlie old     100
      charlie new     400
      dog     old     230
      dog     new     410
      
    2. a append
      Append to file.
      $ cat data
      $a\
      edward  new 410\
      edward  old 345
      
      $ sed -f data sedfile
      able    old     100
      able    new     200
      able    blue    200
      baker   old     300
      baker   new     300
      baker   blue    200
      charlie old     100
      charlie new     400
      charlie blue    400
      dog     old     230
      dog     new     410
      dog     blue    700
      edward  new     410
      edward  old     345
      
    3. i insert
      Insert works the same as append except the data is written before the matched line.
    4. c change
      Change deletes lines and replaces them with text.
      $ cat data
      1,3c\
      edward  new 410\
      edward  old 345
      
      $ sed -f data sedfile
      edward  new 	410
      edward  old 	345
      baker   old 	300
      baker   new 	300
      baker   blue    200
      charlie old 	100
      charlie new 	400
      charlie blue    400
      dog     old 	230
      dog     new 	410
      dog     blue    700
      
    5. s substitute.
      The substitute function changes part of selected lines.
      $sed 's/blue/red/' sedfile
      able    old 	100
      able    new 	200
      able    red 	200
      baker   old 	300
      baker   new 	300
      baker   red 	200
      charlie old 	100
      charlie new 	400
      charlie red 	400
      dog     old 	230
      dog     new 	410
      dog     red 	700
      
      The substitute command uses the g, p and w flags.
      g
      The g is global. Replace all occurrences of the pattern in the line.
      $ sed 's/0//g' sedfile
      able    old     1
      able    new     2
      able    blue    2
      baker   old 	3
      baker   new 	3
      baker   blue    2
      charlie old 	1
      charlie new 	4
      charlie blue    4
      dog     old 	23
      dog     new 	41
      dog     blue    7
      
      p
      The p flag prints the line only if a replace is made.
      $ sed -n 's/old/y2k/p' sedfile
      able    y2k 100
      baker   y2k 300
      charlie y2k 100
      dog     y2k 230
      
      w
      The w flag write the line to a line if the line is altered.
      $ sed  's/old/y2k/w new' sedfile  
      able    y2k 	100
      able    new 	200
      able    blue    200
      baker   y2k 	300
      baker   new 	300
      baker   blue    200
      charlie y2k 	100
      charlie new 	400
      charlie blue    400
      dog     y2k 	230
      dog     new 	410
      dog     blue    700
      
      $ cat new
      able    y2k 	100
      baker   y2k 	300
      charlie y2k 	100
      dog     y2k 	230
      


  8. Input and output functions.
    p
    Print the matched line is usually used in conjunction with the -n option.
    w
    Write specified line to the file.
    $ sed '1,5w new' sedfile
    able    old 	100
    able    new 	200
    able    blue    200
    baker   old 	300
    baker   new 	300
    baker   blue    200
    charlie old 	100
    charlie new 	400
    charlie blue    400
    dog     old 	230
    dog     new 	410
    dog     blue    700
    
    $ cat new
    able    old 	100
    able    new 	200
    able    blue    200
    baker   old 	300
    baker   new 	300
    
    r
    Reads a file and appends the text.
    $ cat new  
    mable   old 	100
    mable   new 	200
    mable   blue    200
    
    $ sed '3r new' sedfile
    able    old 	100
    able    new 	200
    able    blue    200
    mable   old 	100
    mable   new 	200
    mable   blue    200
    baker   old 	300
    baker   new 	300
    baker   blue    200
    charlie old 	100
    charlie new 	400
    charlie blue    400
    dog     old 	230
    dog     new 	410
    dog     blue    700
    

© Allan Kochis Last revision 2/14/2000