Next: , Previous: , 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-chars with the corresponding character in dest-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 given y command.)

Instances of the / (or whatever other character is used in its stead), \, or newlines can appear in the source-chars or dest-chars lists, provide that each instance is escaped by a \. The source-chars and dest-chars lists must contain the same number of characters (after de-escaping).

See the tr command from GNU coreutils for similar functionality.

a text

Appending text after a line. This is a GNU extension to the standard a command - see below for details.

Example: Add the word ‘hello’ after the second line:

$ seq 3 | sed '2a hello'
1
2
hello
3

Leading whitespaces after the a command are ignored. The text to add is read until the end of the line.

a\
text

Appending text after a line.

Example: Add ‘hello’ after the second line (-| indicates printed output lines):

$ seq 3 | sed '2a\
hello'
-|1
-|2
-|hello
-|3

The a command 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 text are processed, so you should use \\ in text to 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 a command and text can be separated into two -e parameters, enabling easier scripting:

$ seq 3 | sed -e '2a\' -e hello
1
2
hello
3

$ sed -e '2a\' -e "$VAR"
i text

insert text before a line. This is a GNU extension to the standard i command - see below for details.

Example: Insert the word ‘hello’ before the second line:

$ seq 3 | sed '2i hello'
1
hello
2
3

Leading whitespaces after the i command are ignored. The text to add is read until the end of the line.

i\
text

Immediately 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 text are processed, so you should use \\ in text to 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 i command and text can be separated into two -e parameters, enabling easier scripting:

$ seq 3 | sed -e '2i\' -e hello
1
hello
2
3

$ sed -e '2i\' -e "$VAR"
c text

Replaces the line(s) with text. This is a GNU extension to the standard c command - 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 whitespaces after the c command are ignored. The text to add is read until the end of the line.

c\
text

Delete 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 c starts 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 c command and text can be separated into two -e parameters, 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 n

Print 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 $.

n specifies 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. The n parameter is a GNU sed extension.

r filename

Reads text file a file. Example:

$ seq 3 | sed '2r/etc/hostname'
1
2
fencepost.gnu.org
3

Queue the contents of filename to 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 if filename cannot be read, it is treated as if it were an empty file, without any error indication.

As a GNU sed extension, the special value /dev/stdin is 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 filename

Write the pattern space to filename. As a GNU sed extension, two special values of file-name are supported: /dev/stderr, which writes the result to the standard error, and /dev/stdout, which writes to the standard output.4

The file will be created (or truncated) before the first input line is read; all w commands (including instances of the w flag on successful s commands) which refer to the same filename are output without closing and reopening the file.

D

If pattern space contains no newline, start a normal new cycle as if the d command 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.

N

Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then sed exits without processing any more commands.

When -z is used, a zero byte (the ascii ‘NUL’ character) is added between the lines (instead of a new line).

By default sed does 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.

P

Print out the portion of the pattern space up to the first newline.

h

Replace the contents of the hold space with the contents of the pattern space.

H

Append a newline to the contents of the hold space, and then append the contents of the pattern space to that of the hold space.

g

Replace the contents of the pattern space with the contents of the hold space.

G

Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space.

x

Exchange the contents of the hold and pattern spaces.


Footnotes

(4)

This is equivalent to p unless the -i option is being used.


Next: , Previous: , Up: sed scripts   [Contents][Index]