Other Commands (sed, a stream editor)
Next: Programming Commands, Previous: Common Commands, Up: sed scripts [Contents][Index]
3.5 Less Frequently-Used Commands
Though perhaps less frequently used than those in the previous section, some very small yet useful sed scripts can be built with these commands.
y/source-chars/dest-chars/Transliterate any characters in the pattern space which match any of the
source-charswith the corresponding character indest-chars.Example: transliterate ‘
a-j’ into ‘0-9’:$ echo hello world | sed 'y/abcdefghij/0123456789/' 74llo worl3
(The
/characters may be uniformly replaced by any other single character within any givenycommand.)Instances of the
/(or whatever other character is used in its stead),\, or newlines can appear in thesource-charsordest-charslists, provide that each instance is escaped by a\. Thesource-charsanddest-charslists must contain the same number of characters (after de-escaping).See the
trcommand from GNU coreutils for similar functionality.a textAppending
textafter a line. This is a GNU extension to the standardacommand - see below for details.Example: Add the word ‘
hello’ after the second line:$ seq 3 | sed '2a hello' 1 2 hello 3
Leading whitespace after the
acommand is ignored. The text to add is read until the end of the line.a\
textAppending
textafter a line.Example: Add ‘
hello’ after the second line (-| indicates printed output lines):$ seq 3 | sed '2a\ hello' -|1 -|2 -|hello -|3
The
acommand queues the lines of text which follow this command (each but the last ending with a\, which are removed from the output) to be output at the end of the current cycle, or when the next input line is read.As a GNU extension, this command accepts two addresses.
Escape sequences in
textare processed, so you should use\\intextto print a single backslash.The commands resume after the last line without a backslash (
\) - ‘world’ in the following example:$ seq 3 | sed '2a\ hello\ world 3s/./X/' -|1 -|2 -|hello -|world -|X
As a GNU extension, the
acommand andtextcan be separated into two-eparameters, enabling easier scripting:$ seq 3 | sed -e '2a\' -e hello 1 2 hello 3 $ sed -e '2a\' -e "$VAR"
i textinsert
textbefore a line. This is a GNU extension to the standardicommand - see below for details.Example: Insert the word ‘
hello’ before the second line:$ seq 3 | sed '2i hello' 1 hello 2 3
Leading whitespace after the
icommand is ignored. The text to add is read until the end of the line.i\
textImmediately output the lines of text which follow this command.
Example: Insert ‘
hello’ before the second line (-| indicates printed output lines):$ seq 3 | sed '2i\ hello' -|1 -|hello -|2 -|3
As a GNU extension, this command accepts two addresses.
Escape sequences in
textare processed, so you should use\\intextto print a single backslash.The commands resume after the last line without a backslash (
\) - ‘world’ in the following example:$ seq 3 | sed '2i\ hello\ world s/./X/' -|X -|hello -|world -|X -|X
As a GNU extension, the
icommand andtextcan be separated into two-eparameters, enabling easier scripting:$ seq 3 | sed -e '2i\' -e hello 1 hello 2 3 $ sed -e '2i\' -e "$VAR"
c textReplaces the line(s) with
text. This is a GNU extension to the standardccommand - see below for details.Example: Replace the 2nd to 9th lines with the word ‘
hello’:$ seq 10 | sed '2,9c hello' 1 hello 10
Leading whitespace after the
ccommand is ignored. The text to add is read until the end of the line.c\
textDelete the lines matching the address or address-range, and output the lines of text which follow this command.
Example: Replace 2nd to 4th lines with the words ‘
hello’ and ‘world’ (-| indicates printed output lines):$ seq 5 | sed '2,4c\ hello\ world' -|1 -|hello -|world -|5
If no addresses are given, each line is replaced.
A new cycle is started after this command is done, since the pattern space will have been deleted. In the following example, the
cstarts a new cycle and the substitution command is not performed on the replaced text:$ seq 3 | sed '2c\ hello s/./X/' -|X -|hello -|X
As a GNU extension, the
ccommand andtextcan be separated into two-eparameters, enabling easier scripting:$ seq 3 | sed -e '2c\' -e hello 1 hello 3 $ sed -e '2c\' -e "$VAR"
=Print out the current input line number (with a trailing newline).
$ printf '%s\n' aaa bbb ccc | sed = 1 aaa 2 bbb 3 ccc
As a GNU extension, this command accepts two addresses.
l nPrint the pattern space in an unambiguous form: non-printable characters (and the
\character) are printed in C-style escaped form; long lines are split, with a trailing\character to indicate the split; the end of each line is marked with a$.nspecifies the desired line-wrap length; a length of 0 (zero) means to never wrap long lines. If omitted, the default as specified on the command line is used. Thenparameter is a GNUsedextension.r filenameReads file
filename. Example:$ seq 3 | sed '2r/etc/hostname' 1 2 fencepost.gnu.org 3
Queue the contents of
filenameto be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that iffilenamecannot be read, it is treated as if it were an empty file, without any error indication.As a GNU
sedextension, the special value/dev/stdinis supported for the file name, which reads the contents of the standard input.As a GNU extension, this command accepts two addresses. The file will then be reread and inserted on each of the addressed lines.
w filenameWrite the pattern space to
filename. As a GNUsedextension, two special values offilenameare supported:/dev/stderr, which writes the result to the standard error, and/dev/stdout, which writes to the standard output.4The file will be created (or truncated) before the first input line is read; all
wcommands (including instances of thewflag on successfulscommands) which refer to the samefilenameare output without closing and reopening the file.DIf pattern space contains no newline, start a normal new cycle as if the
dcommand was issued. Otherwise, delete text in the pattern space up to the first newline, and restart cycle with the resultant pattern space, without reading a new line of input.NAdd a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then
sedexits without processing any more commands.When
-zis used, a zero byte (the ascii ‘NUL’ character) is added between the lines (instead of a new line).By default
seddoes not terminate if there is no ’next’ input line. This is a GNU extension which can be disabled with--posix. See N command on the last line.PPrint out the portion of the pattern space up to the first newline.
hReplace the contents of the hold space with the contents of the pattern space.
HAppend a newline to the contents of the hold space, and then append the contents of the pattern space to that of the hold space.
gReplace the contents of the pattern space with the contents of the hold space.
GAppend a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space.
xExchange the contents of the hold and pattern spaces.
Footnotes
(4)
This is equivalent to p unless the -i option is being used.
Next: Programming Commands, Previous: Common Commands, Up: sed scripts [Contents][Index]