s command (as in substitute) is probably the most important
sed and has a lot of different options. The syntax of
s command is
Its basic concept is simple: the
s command attempts to match
the pattern space against the supplied regular expression regexp;
if the match is successful, then that portion of the
pattern space which was matched is replaced with replacement.
For details about regexp syntax see Regular Expression Addresses.
The replacement can contain
\n (n being
a number from 1 to 9, inclusive) references, which refer to
the portion of the match which is contained between the nth
\( and its matching
Also, the replacement can contain unescaped
characters which reference the whole matched portion
of the pattern space.
characters may be uniformly replaced by any other single
character within any given
s command. The
character (or whatever other character is used in its stead)
can appear in the regexp or replacement
only if it is preceded by a
Finally, as a GNU
sed extension, you can include a
special sequence made of a backslash and one of the letters
The meaning is as follows:
Turn the replacement
to lowercase until a
\E is found,
Turn the next character to lowercase,
Turn the replacement to uppercase
\E is found,
Turn the next character to uppercase,
Stop case conversion started by
g flag is being used, case conversion does not
propagate from one occurrence of the regular expression to
another. For example, when the following command is executed
with ‘a-b-’ in pattern space:
the output is ‘axxB’. When replacing the first ‘-’,
the ‘\u’ sequence only affects the empty replacement of
‘\1’. It does not affect the
x character that is
added to pattern space when replacing
On the other hand,
\u do affect the remainder
of the replacement text if they are followed by an empty substitution.
With ‘a-b-’ in pattern space, the following command:
will replace ‘-’ with ‘X’ (uppercase) and ‘b-’ with ‘Bx’. If this behavior is undesirable, you can prevent it by adding a ‘\E’ sequence—after ‘\1’ in this case.
To include a literal
&, or newline in the final
replacement, be sure to precede the desired
or newline in the replacement with a
s command can be followed by zero or more of the
Apply the replacement to all matches to the regexp, not just the first.
Only replace the numberth match of the regexp.
Note: the POSIX standard does not specify what should happen
when you mix the
g and number modifiers,
and currently there is no widely agreed upon meaning
sed, the interaction is defined to be:
ignore matches before the numberth,
and then match and replace all matches from
the numberth on.
If the substitution was made, then print the new pattern space.
Note: when both the
e options are specified,
the relative ordering of the two produces very different results.
ep (evaluate then print) is what you want,
but operating the other way round can be useful for debugging.
For this reason, the current version of GNU
specially the presence of
p options both before and after
e, printing the pattern space before and after evaluation,
while in general flags for the
s command show their
effect just once. This behavior, although documented, might
change in future versions.
If the substitution was made, then write out the result to the named file.
As a GNU
sed extension, two special values of filename are
supported: /dev/stderr, which writes the result to the standard
error, and /dev/stdout, which writes to the standard
This command allows one to pipe input from a shell command
into pattern space. If a substitution was made, the command
that is found in pattern space is executed and pattern space
is replaced with its output. A trailing newline is suppressed;
results are undefined if the command to be executed contains
a NUL character. This is a GNU
I modifier to regular-expression matching is a GNU
extension which makes
sed match regexp in a
M modifier to regular-expression matching is a GNU
extension which directs GNU
sed to match the regular expression
in multi-line mode. The modifier causes
match respectively (in addition to the normal behavior) the empty string
after a newline, and the empty string before a newline. There are
special character sequences
which always match the beginning or the end of the buffer.
the period character does not match a new-line character in
This is equivalent to
p unless the -i
option is being used.