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
-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.
address1 is only specified, the request is applied only to the lines matching
address2 is specified, the request is applied from the line matching 'address1' to the line matching
Regular expressions for
sed are mostly that of
In addition, it is possible to define groups using parenthesis
(...) (see below).
||print the line|
||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