sed: Other Commands
1
1 3.5 Less Frequently-Used Commands
1 =================================
1
1 Though perhaps less frequently used than those in the previous section,
1 some very small yet useful 'sed' scripts can be built with these
1 commands.
1
1 'y/SOURCE-CHARS/DEST-CHARS/'
1 Transliterate any characters in the pattern space which match any
1 of the SOURCE-CHARS with the corresponding character in DEST-CHARS.
1
1 Example: transliterate 'a-j' into '0-9':
1 $ echo hello world | sed 'y/abcdefghij/0123456789/'
1 74llo worl3
1
1 (The '/' characters may be uniformly replaced by any other single
1 character within any given 'y' command.)
1
1 Instances of the '/' (or whatever other character is used in its
1 stead), '\', or newlines can appear in the SOURCE-CHARS or
1 DEST-CHARS lists, provide that each instance is escaped by a '\'.
1 The SOURCE-CHARS and DEST-CHARS lists _must_ contain the same
1 number of characters (after de-escaping).
1
1 See the 'tr' command from GNU coreutils for similar functionality.
1
1 'a TEXT'
1 Appending TEXT after a line. This is a GNU extension to the
1 standard 'a' command - see below for details.
1
1 Example: Add the word 'hello' after the second line:
1 $ seq 3 | sed '2a hello'
1 1
1 2
1 hello
1 3
1
1 Leading whitespace after the 'a' command is ignored. The text to
1 add is read until the end of the line.
1
1 'a\'
1 'TEXT'
1 Appending TEXT after a line.
1
1 Example: Add 'hello' after the second line (-| indicates printed
1 output lines):
1 $ seq 3 | sed '2a\
1 hello'
1 -|1
1 -|2
1 -|hello
1 -|3
1
1 The 'a' command queues the lines of text which follow this command
1 (each but the last ending with a '\', which are removed from the
1 output) to be output at the end of the current cycle, or when the
1 next input line is read.
1
1 As a GNU extension, this command accepts two addresses.
1
1 Escape sequences in TEXT are processed, so you should use '\\' in
1 TEXT to print a single backslash.
1
1 The commands resume after the last line without a backslash ('\') -
1 'world' in the following example:
1 $ seq 3 | sed '2a\
1 hello\
1 world
1 3s/./X/'
1 -|1
1 -|2
1 -|hello
1 -|world
1 -|X
1
1 As a GNU extension, the 'a' command and TEXT can be separated into
1 two '-e' parameters, enabling easier scripting:
1 $ seq 3 | sed -e '2a\' -e hello
1 1
1 2
1 hello
1 3
1
1 $ sed -e '2a\' -e "$VAR"
1
1 'i TEXT'
1 insert TEXT before a line. This is a GNU extension to the standard
1 'i' command - see below for details.
1
1 Example: Insert the word 'hello' before the second line:
1 $ seq 3 | sed '2i hello'
1 1
1 hello
1 2
1 3
1
1 Leading whitespace after the 'i' command is ignored. The text to
1 add is read until the end of the line.
1
1 'i\'
1 'TEXT'
1 Immediately output the lines of text which follow this command.
1
1 Example: Insert 'hello' before the second line (-| indicates
1 printed output lines):
1 $ seq 3 | sed '2i\
1 hello'
1 -|1
1 -|hello
1 -|2
1 -|3
1
1 As a GNU extension, this command accepts two addresses.
1
1 Escape sequences in TEXT are processed, so you should use '\\' in
1 TEXT to print a single backslash.
1
1 The commands resume after the last line without a backslash ('\') -
1 'world' in the following example:
1 $ seq 3 | sed '2i\
1 hello\
1 world
1 s/./X/'
1 -|X
1 -|hello
1 -|world
1 -|X
1 -|X
1
1 As a GNU extension, the 'i' command and TEXT can be separated into
1 two '-e' parameters, enabling easier scripting:
1 $ seq 3 | sed -e '2i\' -e hello
1 1
1 hello
1 2
1 3
1
1 $ sed -e '2i\' -e "$VAR"
1
1 'c TEXT'
1 Replaces the line(s) with TEXT. This is a GNU extension to the
1 standard 'c' command - see below for details.
1
1 Example: Replace the 2nd to 9th lines with the word 'hello':
1 $ seq 10 | sed '2,9c hello'
1 1
1 hello
1 10
1
1 Leading whitespace after the 'c' command is ignored. The text to
1 add is read until the end of the line.
1
1 'c\'
1 'TEXT'
1 Delete the lines matching the address or address-range, and output
1 the lines of text which follow this command.
1
1 Example: Replace 2nd to 4th lines with the words 'hello' and
1 'world' (-| indicates printed output lines):
1 $ seq 5 | sed '2,4c\
1 hello\
1 world'
1 -|1
1 -|hello
1 -|world
1 -|5
1
1 If no addresses are given, each line is replaced.
1
1 A new cycle is started after this command is done, since the
1 pattern space will have been deleted. In the following example,
1 the 'c' starts a new cycle and the substitution command is not
1 performed on the replaced text:
1
1 $ seq 3 | sed '2c\
1 hello
1 s/./X/'
1 -|X
1 -|hello
1 -|X
1
1 As a GNU extension, the 'c' command and TEXT can be separated into
1 two '-e' parameters, enabling easier scripting:
1 $ seq 3 | sed -e '2c\' -e hello
1 1
1 hello
1 3
1
1 $ sed -e '2c\' -e "$VAR"
1
1 '='
1 Print out the current input line number (with a trailing newline).
1
1 $ printf '%s\n' aaa bbb ccc | sed =
1 1
1 aaa
1 2
1 bbb
1 3
1 ccc
1
1 As a GNU extension, this command accepts two addresses.
1
1 'l N'
1 Print the pattern space in an unambiguous form: non-printable
1 characters (and the '\' character) are printed in C-style escaped
1 form; long lines are split, with a trailing '\' character to
1 indicate the split; the end of each line is marked with a '$'.
1
1 N specifies the desired line-wrap length; a length of 0 (zero)
1 means to never wrap long lines. If omitted, the default as
1 specified on the command line is used. The N parameter is a GNU
1 'sed' extension.
1
1 'r FILENAME'
1
1 Reads file FILENAME. Example:
1
1 $ seq 3 | sed '2r/etc/hostname'
1 1
1 2
1 fencepost.gnu.org
1 3
1
1 Queue the contents of FILENAME to be read and inserted into the
1 output stream at the end of the current cycle, or when the next
1 input line is read. Note that if FILENAME cannot be read, it is
1 treated as if it were an empty file, without any error indication.
1
1 As a GNU 'sed' extension, the special value '/dev/stdin' is
1 supported for the file name, which reads the contents of the
1 standard input.
1
1 As a GNU extension, this command accepts two addresses. The file
1 will then be reread and inserted on each of the addressed lines.
1
1 'w FILENAME'
1 Write the pattern space to FILENAME. As a GNU 'sed' extension, two
1 special values of FILENAME are supported: '/dev/stderr', which
1 writes the result to the standard error, and '/dev/stdout', which
1 writes to the standard output.(1)
1
1 The file will be created (or truncated) before the first input line
1 is read; all 'w' commands (including instances of the 'w' flag on
1 successful 's' commands) which refer to the same FILENAME are
1 output without closing and reopening the file.
1
1 'D'
1 If pattern space contains no newline, start a normal new cycle as
1 if the 'd' command was issued. Otherwise, delete text in the
1 pattern space up to the first newline, and restart cycle with the
1 resultant pattern space, without reading a new line of input.
1
1 'N'
1 Add a newline to the pattern space, then append the next line of
1 input to the pattern space. If there is no more input then 'sed'
1 exits without processing any more commands.
1
1 When '-z' is used, a zero byte (the ascii 'NUL' character) is added
1 between the lines (instead of a new line).
1
1 By default 'sed' does not terminate if there is no 'next' input
1 line. This is a GNU extension which can be disabled with
1 '--posix'. ⇒N command on the last line N_command_last_line.
1
1 'P'
1 Print out the portion of the pattern space up to the first newline.
1
1 'h'
1 Replace the contents of the hold space with the contents of the
1 pattern space.
1
1 'H'
1 Append a newline to the contents of the hold space, and then append
1 the contents of the pattern space to that of the hold space.
1
1 'g'
1 Replace the contents of the pattern space with the contents of the
1 hold space.
1
1 'G'
1 Append a newline to the contents of the pattern space, and then
1 append the contents of the hold space to that of the pattern space.
1
1 'x'
1 Exchange the contents of the hold and pattern spaces.
1
1 ---------- Footnotes ----------
1
1 (1) This is equivalent to 'p' unless the '-i' option is being used.
1