sed is a non interactive file editor that can filter and transform text.

It reads the lines of a file one by one (sequentially) and applies operations on them.


sed [-n] [-e request] [file]

sed copies the file(s) in argument (by default, the standard input) to the standard output, and applies to each line the request(s) that are preceded by the -e option (one request per -e).

The -n option requests to output to the standard output only the lines that have been processed.

Each request can be applied to all or part of the file. To do so, addressing must be specified.


Without addressing, the request is applied to all lines of all input files.

Addresses can be

  • absolute numbers referring to line numberings in files,

  • or the special character $ refering to the last line,

  • or a regular expression between two slashes that defines a pattern to be matched by the addressed line.

If address1 is only specified, the request is applied only to the lines matching address1.

If address2 is specified, the request is applied from the line matching 'address1' to the line matching address2.

Regular expressions for sed are mostly that of grep.

In addition, it is possible to define groups using parenthesis (...) (see below).

sed request action
s/pattern1/pattern2/option replace pattern1 by pattern2
The g option applies the replacement to all occurrences of pattern1 in the line
The p option request to print only the lines for which a replacement has been made
p print the line
d delete the line


  • initial data file:
Mitterand Francois
Chirac Jacques
Sarkozy Nicolas
Hollande Francois
  • sed code to reverse name and firstname:
sed -e '1,$s/^\([A-Za-z]*\)  *\([A-Za-z]*\)$/\2 \1/' president.txt