sed program consists of one or more
passed in by one or more of the
options, or the first non-option argument if zero of these
options are used.
This document will refer to “the”
this is understood to mean the in-order concatenation
of all of the
script-files passed in.
sed commands follow this syntax:
X is a single-letter
[addr] is an optional line address. If
[addr] is specified,
X will be executed only on the matched lines.
[addr] can be a single line number, a regular expression,
or a range of lines (see sed addresses).
[options] are used for some
The following example deletes lines 30 to 35 in the input.
30,35 is an address range.
d is the delete command:
sed '30,35d' input.txt > output.txt
The following example prints all input until a line
starting with the word ‘
foo’ is found. If such line is found,
sed will terminate with exit status 42.
If such line was not found (and no other error occurred),
will exit with status 0.
/^foo/ is a regular-expression address.
q is the quit command.
42 is the command option.
sed '/^foo/q42' input.txt > output.txt
Commands within a
script-file can be
separated by semicolons (
;) or newlines (ASCII 10).
Multiple scripts can be specified with
The following examples are all equivalent. They perform two
operations: deleting any lines matching the regular expression
and replacing all occurrences of the string ‘
hello’ with ‘
sed '/^foo/d ; s/hello/world/' input.txt > output.txt sed -e '/^foo/d' -e 's/hello/world/' input.txt > output.txt echo '/^foo/d' > script.sed echo 's/hello/world/' >> script.sed sed -f script.sed input.txt > output.txt echo 's/hello/world/' > script2.sed sed -e '/^foo/d' -f script2.sed input.txt > output.txt
i, due to their syntax,
cannot be followed by semicolons working as command separators and
thus should be terminated
with newlines or be placed at the end of a
Commands can also be preceded with optional non-significant
See Multiple commands syntax.