The "s" Command (sed, a stream editor)
Next: Common Commands, Previous: sed commands list, Up: sed scripts [Contents][Index]
3.3 The s Command
The s command (as in substitute) is probably the most important in sed and has a lot of different options. The syntax of the s command is ‘s/regexp/replacement/flags’.
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.
The / 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 \ character.
Finally, as a GNU sed extension, you can include a special sequence made of a backslash and one of the letters L, l, U, u, or E. The meaning is as follows:
\L- Turn the replacement to lowercase until a
\Uor\Eis found, \l- Turn the next character to lowercase,
\U- Turn the replacement to uppercase until a
\Lor\Eis found, \u- Turn the next character to uppercase,
\E- Stop case conversion started by
\Lor\U.
When the 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:
s/\(b\?\)-/x\u\1/g
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 b- with xB.
On the other hand, \l and \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:
s/\(b\?\)-/\u\1x/g
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 \.
The s command can be followed by zero or more of the following flags:
gApply the replacement to all matches to the
regexp, not just the first.numberOnly replace the
numberth match of theregexp.interaction in
scommand Note: the POSIX standard does not specify what should happen when you mix thegandnumbermodifiers, and currently there is no widely agreed upon meaning acrosssedimplementations. For GNUsed, the interaction is defined to be: ignore matches before thenumberth, and then match and replace all matches from thenumberth on.pIf the substitution was made, then print the new pattern space.
Note: when both the
pandeoptions are specified, the relative ordering of the two produces very different results. In general,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 GNUsedinterprets specially the presence ofpoptions both before and aftere, printing the pattern space before and after evaluation, while in general flags for thescommand show their effect just once. This behavior, although documented, might change in future versions.w filenameIf the substitution was made, then write out the result to the named file. As a GNU
sedextension, two special values offilenameare supported:/dev/stderr, which writes the result to the standard error, and/dev/stdout, which writes to the standard output.3eThis 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
sedextension.I
iThe
Imodifier to regular-expression matching is a GNU extension which makessedmatchregexpin a case-insensitive manner.M
mThe
Mmodifier to regular-expression matching is a GNUsedextension which directs GNUsedto match the regular expression in multi-line mode. The modifier causes^and$to 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 (\`and\') which always match the beginning or the end of the buffer. In addition, the period character does not match a new-line character in multi-line mode.
Footnotes
(3)
This is equivalent to p unless the -i option is being used.
Next: Common Commands, Previous: sed commands list, Up: sed scripts [Contents][Index]