The substitution functions change parts of lines selected by a
context search within the line, using the syntax:
The s function replaces part of a line selected by the designated pattern with the replacement pattern. This is similar to vi (see ``A quick tour of vi''). The substitution is restricted to only those lines matching the optional address. The pattern argument contains a pattern, exactly like the patterns in addresses. The only difference between a pattern and a context address is that a pattern argument may be delimited by any character other than space or newline. By default, only the first string matched by the pattern is replaced, except when you use the g flag.
The replacement argument begins immediately after the second delimiting character of the pattern, and must be followed immediately by another instance of the delimiting character.
The replacement is not a pattern, and the characters that are special in patterns do not have special meaning in replacement. Instead, the following characters are special:
This command substitutes the word ``fruit'' with ``fresh fruit & vegetables''.
This substitution consists of a regular expression in two groups;
``foo'' and ``bar''. If applied to a line containing the word
``foobar'' the regular expression matches each of the grouped
subexpressions in turn. The replacement argument ``\2\1''
transposes the order of the first two grouped expressions that
sed matched, producing the following output:
A flag argument can contain the following:
The following example replaces all occurrences of the word
``password'' with a string of x's:
This command substitutes the second instance of ``Current'' in each
input line with ``Expired''. The default value of n is 1,
which is why a substitution operation specified without the global
flag affects only the first instance of a matching string (see the
The maximum value of n is 512.
Here are some examples of the commands in use. When applied to the /etc/passwd file used previously, /charlie/s/charlie/charles/w changes sends the following to standard output:
root:x:0:0:Superuser:/: remacc:x:::Remote access:: daemon:No login:1:1:Spooler:/usr/spool: sys:No login:2:2:System information:: bin:x:3:3:System administrator:/usr/src: xmail:x:4:4:Secret Mail:/usr/spool/pubkey: msgs:No login:7:7:System messages:/usr/msgs: charles:x:8:5:Charles Stross:/usr/charlie:/bin/kshAt the same time, the following is written to the file changes:
charles:x:8:5:Charles Stross:/usr/charlie:/bin/ksh(This might be used, for example, to change the login name of the user ``charlie'' to ``charles''.)
The following is a shorthand version of the same command:
In this case, the context address is the same as the string to be substituted. In this case, sed assumes that the value of the null field is the same as that of the preceding (address) field.
The command s/:/\<Tab>/gp (where <Tab> is a typed tab character) replaces all colons with tabs in a data file, and prints the result (for example, as input to an awk program).
Note that where a substitution command involves a literal backslash,
it must be quoted, as follows:
$ sed -e "s/<Tab>/\\\\/gp" input_file
This replaces all the tabs in the input file with a single
backslash. Note also the following:
$ sed -e 's/<Tab>/\\/gp' input_file
This has the same effect. The difference between the two quoting mechanisms is that the double quotes prevent the expansion of all special characters except the backslash, the dollar sign and the single quote.
Note that it is not essential that the character used to separate
the fields in the substitute command always be a slash. Consider the
case where a pathname is to be substituted with another
pathname. In such a case, the slashes in the pathname substitution
strings conflict with those used to build up the command
itself. This can be avoided by using another string as the delimiter
character. The following example uses the exclamation mark (!) to
specify a substitution command where the affected strings are