René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

sed, a stream editor

sed sed-programm input-file
sed -f sed-programm-file input-file
sed -f sed-programm-file -e sed-program input-file
The -f flag can be used to specify a file containing the sed programm.

Command line options

-e

-n

Turns off the default behaviour which prints every line. If this switch is used, a line must be explicitly printed for output.

Sed programm

A sed programm consists of 0, 1 or more functions.

function list

The following form applies function to all lines:
function [arguments] [\]
The following form applies function only to the lines that match addresses:
addresses function [arguments] [\]
An example for an address is $ which matches the last line. Together with the = function, the lines in a file can be counted
The following form applies function only to the lines between the line that matches first_address and the line that matches last_address
first_address,last_address function [arguments] [\]
Sed reads the input file line for line. The sed programm is applied to each line.
Here's an example that makes use of this feature.

Grouping commands

Commands can be grouped with curly braces. The closing } must be the only item on a line.
This makes it possible to execute multiple commands for a matching address.
addr_1[,addr2] {
  ..
  ..
}

Functions

s

The substitution function.
s/regexp/substitution/x
x is nothing or one of the following:
  • N
    N being a number: replaces the Nth occurance of regexp.
  • g
    Replaces all occurences of regexp. The default is to only replace the first.
  • p
    Writes pattern space only if substituion was successful.
  • w file-name
    Writes the pattern space to the file file-name if substituion was successful.

i

The insert function.

a

The append function.

d

The delete function. Compare to D
>After a line has been deleted, the next line will be read from the input file and the script re-starts at the beginning of the script.

d_

The delete up to '\n' function. Deletes the beginning up to the first new line in the pattern space. Skips the rest of the script and goes to its beginning again. Compare to d.
After a line has been deleted, the next line will be read from the input file and the script re-starts at the beginning of the script.

c

The 'change the pattern space' function. Replaces the current pattern space with some text.

n

The (read) next line function.
Compare with N.
The current value of the pattern space is output, the pattern space is filled with the next line of the input file, but the script is not re-started at the beginning.

N

The (add) next line function.
adds a '\n' plus the current line to the pattern space.
Compare with n.

p

The print command. Prints the pattern space's content. Compare to P.

P

The print command. Prints the pattern space's content up to the first '\n'. Compare to P.

h

Copies (overwriting) the hold area with the pattern space's content.

H

Appends the pattern space's content to the hold area.

g

Gets the hold area's content.

G

Appends the hold area's content to the pattern space.

x

exchanges the hold area's content with the pattern space's content.

t

Branch to a a label if the previous substition was successful.

b

Branch to a a label.

q

Quits sed script.

w

Write pattern space to file.

=

Prints the current line number.

:

Name a label to which can be jumped with t or b

Examples

Writing to files

The following script creates three files (/tmp/f, /tmp/o and /tmp/f) and writes lines according to their first letter into those files:
echo 'one eins un
two zwei deux
three drei trois
four four quattre
five fuenf cinque' | sed '
 /^f/w /tmp/f
 /^t/w /tmp/t
 /^o/w /tmp/o'

Swapping two lines with sed

Assume, in the following file I want to swap the third with the fifth line:
one
two
five
four
three
six
seven
Then this could be done with the following sed script:
sed -ne '
 3!{p;d;}
 h;n;:1
 4!{N;b1}
 G;h;n;p;g;p' numbers
Note, it's a 4, not a 5 in the sed script!

Links

Splitting $PATH so that each component is printed on a single line.