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


3.2 sed commands summary

The following commands are supported in GNU sed. Some are standard POSIX commands, while other are GNU extensions. Details and examples for each command are in the following sections. (Mnemonics) are shown in parentheses.

a\
text

Append text after a line.

a text

Append text after a line (alternative syntax).

b label

Branch unconditionally to label. The label may be omitted, in which case the next cycle is started.

c\
text

Replace (change) lines with text.

c text

Replace (change) lines with text (alternative syntax).

d

Delete the pattern space; immediately start next cycle.

D

If pattern space contains newlines, 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.

If pattern space contains no newline, start a normal new cycle as if the d command was issued.

e

Executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed.

e command

Executes command and sends its output to the output stream. The command can run across multiple lines, all but the last ending with a back-slash.

F

(filename) Print the file name of the current input file (with a trailing newline).

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.

h

(hold) 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.

i\
text

insert text before a line.

i text

insert text before a line (alternative syntax).

l

Print the pattern space in an unambiguous form.

n

(next) If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then sed exits without processing any more commands.

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.

p

Print the pattern space.

P

Print the pattern space, up to the first <newline>.

q[exit-code]

(quit) Exit sed without processing any more commands or input.

Q[exit-code]

(quit) This command is the same as q, but will not print the contents of pattern space. Like q, it provides the ability to return an exit code to the caller.

r filename

Reads text file a file. Example:

R filename

Queue a line 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.

s/regexp/replacement/[flags]

(substitute) Match the regular-expression against the content of the pattern space. If found, replace matched string with replacement.

t label

(test) Branch to label only if there has been a successful substitution since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started.

T label

(test) Branch to label only if there have been no successful substitutions since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started.

v [version]

(version) This command does nothing, but makes sed fail if GNU sed extensions are not supported, or if the requested version is not available.

w filename

Write the pattern space to filename.

W filename

Write to the given filename the portion of the pattern space up to the first newline

x

Exchange the contents of the hold and pattern spaces.

y/src/dst/

Transliterate any characters in the pattern space which match any of the source-chars with the corresponding character in dest-chars.

z

(zap) This command empties the content of pattern space.

#

A comment, until the next newline.

{ cmd ; cmd ... }

Group several commands together.

=

Print the current input line number (with a trailing newline).

: label

Specify the location of label for branch commands (b, t, T).


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