Sed


Sed is a Unix utility that parses and transforms text, using a simple, compact programming language

Glossary

Usage

sed <rule> <file>
sed -e <rule1> -e <rule2> -e <rule3> <file>
sed -f <script> <file>
sed -f <script> <file1> <file2> <file3>
sed -n -f <script> <file>

Comments

Lines starting with #.

Grouping

You may nest commands by surrounding them in curly braces. For example, this command deletes lines containing bar and replaces every occurrence of baz with qux only on lines in betwee lines that start with START and END:

/^START/,/^END/ {
  /bar/ d
  s/baz/qux/g
}

Commands

substitute (s)

[address] s/pattern/replacement/flags

replacement

Characters with special meaning:

flags

This is commonly used in instances where the regular expression repeats itself on a line.

This is commonly used when the default output is suppressed (-n)

Extras

s!/foo/bar!/bar/baz!g

delete (d)

[address] d

insert (i)

[address] i\
<line1>\
<line2>\
...\
<lineN>

Insert the supplied text before the current line in the pattern space.

append (a)

[address] a\
<line1>\
<line2>\
...\
<lineN>

Insert the supplied text after the current line in the pattern space.

change (c)

[address] c\
<line1>\
<line2>\
...\
<lineN>

Replace the line range specified by the pattern space.

list (l)

Displays the contents of the pattern space, showing non-printing characters as two-digit ASCII codes.

Example:

cat file | sed -n "l"

transform (y)

[address] y/abc/xyz/

Transform each character by string position. In the above example, a is transformed to x, b to y, and c to z.

[address] p

Causes the contents of the pattern space to be printed.

[address] =

Prints the line number of the matched line.

next (n)

[address] n

Outputs the contents of the pattern space and then reads the next line of input without returning to the top of the script.

For example, delete a blank line after a troff header (.H1 <text>):

/^\.H1/ {
  n
  /^$/ d
}

read (r)

[address] r <file>

Read the contents of file into the pattern space after the address line.

write (w)

[address] w <file>

Write the contents of the pattern space into file.

quit (q)

[address] q

Stop reading new input lines.

append next line (N)

[address] N

Add the next line to the current pattern space. The pattern space will be separated by \n characters.

multiline delete (D)

[address] D

Delete a portion of the pattern space, up to the first embedded \n, and it returns to the top of the script.

multiline print (P)

[address] P

Prints a portion of the pattern space, up to the first embedded \n. The remaining parts of the pattern space are automatically printed after the last command.

hold (h)

[address] h

Copy the contents of the pattern space to the hold space.

hold (H)

[address] H

Append the contents of the pattern space to the hold space.

get (g)

[address] g

Copy the contents of the hold space to the pattern space.

get (G)

[address] G

Append the contents of the hold space to the pattern space.

exchange (x)

[address] x

Exchange the contents of the hold space and the pattern space.

branch (b)

[address] b [label]

For example:

:loop
command1
command2
[address] b loop

test (t)

[address] t [label]

Branch to label if a successful substitution has been made on the currently addressed line.

[address] {
  s/<pattern1>/<substitution1>
  t label1
  s/<pattern2>/<substitution2>
  t label2
}

:label1
...

:label2
...

Cookbook

substitute (s)

s/pattern/replacement/
s/pattern/\
/
s/pattern/replacement/g
/address/ s/pattern/replacement/g
/address1/,/address2/ s/pattern/replacement/g
N s/pattern/replacement/g
1,10 s/pattern/replacement/g
/address/ !s/pattern/replacement/g
s/pattern/(&)/

delete (d)

N d
$ d
N, d
/address/ !d

‘append next line (N)’

/^$/ {
  N
  /^\n$ D
}

Tips & Tricks

Caveats