Linux in a NutshellLinux in a NutshellSearch this book

12.4. Alphabetical Summary of sed Commands

#

#

Begin a comment in a sed script. Valid only as the first character of the first line. (Some versions of sed, including the GNU version on Linux, allow comments anywhere, but it is better not to rely on this.) If the first line of the script is #n, sed behaves as if -n had been specified.

:

:label

Label a line in the script for the transfer of control by b or t. label may contain up to seven characters.

=

[/pattern/]=

Write to standard output the line number of each line containing pattern.

a

[address]a\
text

Append text following each line matched by address. If text goes over more than one line, newlines must be "escaped" by preceding them with a backslash. The text will be terminated by the first newline that is not escaped in this way. The text is not available in the pattern space, and subsequent commands cannot be applied to it. The results of this command are sent to standard output when the list of editing commands is finished, regardless of what happens to the current line in the pattern space.

Example

$a\
This goes after the last line in the file\
(marked by $). This text is escaped at the\
end of each line, except for the last one.
b

[address1[,address2]]b[label]

Transfer control unconditionally to :label elsewhere in script. That is, the command following the label is the next command applied to the current line. If no label is specified, control falls through to the end of the script, so no more commands are applied to the current line.

Example

Ignore lines between those that begin with .TS and .TE; resume script after .TE:

/^\.TS/,/^\.TE/b
c

[address1[,address2]]c\text

Replace the lines selected by the address with text. When a range of lines is specified, all lines as a group are replaced by a single copy of text. The newline following each line of text must be escaped by a backslash, except the last line. The contents of the pattern space are in effect deleted, and no subsequent editing commands can be applied.

Example

Replace first 100 lines in a file:

1,100c\
\
<First 100 names to be supplied>
d

[address1[,address2]]d

Delete the addressed line (or lines) from the pattern space. Thus, the line is not passed to standard output. A new line of input is read, and editing resumes with the first command in the script.

Example

Delete all blank lines (those whose first character is a line ending):

/^$/d
D

[address1[,address2]]D

Delete first part (up to embedded newline) of multiline pattern space created by the N command, and resume editing with first command in script. If this command empties the pattern space, then a new line of input is read, as if d had been executed.

Example

Strip multiple blank lines, leaving only one:

/^$/{
N
/^\n$/D
}
g

[address1[,address2]]g

Paste the contents of the hold space (see h and H) back into the pattern space, wiping out the previous contents of the pattern space. The example shows a simple way to copy lines.

Example

This script collects all lines containing the word Item: and copies them to a place marker later in the file. The place marker is overwritten.

/Item:/H
/<Replace this line with the item list>/g
G

[address1[,address2]]G

Same as g, except the hold space is pasted below the address instead of overwriting it. The example shows a simple way to cut and paste lines.

Example

This script collects all lines containing the word Item: and moves them after a place marker later in the file. The original Item: lines are deleted.

/Item:/{
H
d
}
/Summary of items:/G
h

[address1[,address2]]h

Copy the pattern space into the hold space, a special temporary buffer. The previous contents of the hold space are obliterated. You can use h to save a line before editing it.

Example

# Edit a line; print the change; replay the original
/Linux/{
h
s/.* Linux \(.*\) .*/\1:/
p
x
}

Sample input:

This describes the Linux ls command.
This describes the Linux cp command.

Sample output:

ls:
This describes the Linux ls command.
cp:
This describes the Linux cp command.
H

[address1[,address2]]H

Append the contents of the pattern space (preceded by a newline) to the contents of the hold space. Even if the hold space is empty, H still appends a newline. H is like an incremental copy. See examples under g and G.

i

[address1]i\
text

Insert text before each line matched by address. (See a for details on text.)

Example

/Item 1/i\
The five items are listed below:
l

[address1[,address2]]l

List the contents of the pattern space, showing nonprinting characters as ASCII codes. Long lines are wrapped.

n

[address1[,address2]]n

Read next line of input into pattern space. The current line is sent to standard output, and the next line becomes the current line. Control passes to the command following n instead of resuming at the top of the script.

Example

In the ms macros, a section header occurs on the line below an .NH macro. To print all lines of header text, invoke this script with sed -n:

/^\.NH/{
n
p
}
N

[address1[,address2]]N

Append next input line to contents of pattern space; the two lines are separated by an embedded newline. (This command is designed to allow pattern matches across two lines.) Using \n to match the embedded newline, you can match patterns across multiple lines. See example at D.

Examples

Like previous example, but print .NH line as well as header title:

/^\.NH/{
N
p
}

Join two lines (replace newline with space):

/^\.NH/{
N
s/\n/ /
p
}
p

[address1[,address2]]p

Print the addressed lines. Unless the -n command-line option is used, this command causes duplicate lines to be output. Also, it is typically used before commands that change flow control (d, N, b) and that might prevent the current line from being output. See examples at h, n, and N.

P

[address1[,address2]]P

Print first part (up to embedded newline) of multiline pattern created by N command. Same as p if N has not been applied to a line.

q

[address]q

Quit when address is encountered. The addressed line is first written to output (if default output is not suppressed), along with any text appended to it by previous a or r commands.

Examples

Delete everything after the addressed line:

/Garbled text follows:/q

Print only the first 50 lines of a file:

50q
r

[address]r file

Read contents of file and append after the contents of the pattern space. One space must be put between the r and the filename.

Example

/The list of items follows:/r item_file
s

[address1[,address2]]s/pattern/replacement/[flags]

Substitute replacement for pattern on each addressed line. If pattern addresses are used, the pattern // represents the last pattern address specified. The following flags can be specified:

n
Replace nth instance of /pattern/ on each addressed line. n is any number in the range 1 to 512; the default is 1.

g
Replace all instances of /pattern/ on each addressed line, not just the first instance.

I
Case insensitive match. This flag is available only on GNU versions of sed.

p
Print the line if a successful substitution is done. If several successful substitutions are done, multiple copies of the line will be printed.

w file
Write the line to a file if a replacement was done.

Examples

Here are some short, commented scripts:

# Change third and fourth quote to ( and ):
/function/{
s/"/(/3
s/"/)/4
}

# Remove all quotes on a given line:
/Title/s/"//g

# Remove first colon or all quotes; print resulting lines:
s/://p
s/"//gp

# Change first "if" but leave "ifdef" alone:
/ifdef/!s/if/   if/
t

[address1[,address2]]t [label]

Test if any substitutions have been made on addressed lines and, if so, branch to line marked by :label. (See b and :.) If label is not specified, control falls through to bottom of script. The t command is like a case statement in the C programming language or the shell programming languages. You test each case; when it's true, you exit the construct.

Example

Suppose you want to fill empty fields of a database. You have this:

ID: 1   Name: greg   Rate: 45
ID: 2   Name: dale
ID: 3

and you want this:

ID: 1   Name: greg   Rate: 45   Phone: ??
ID: 2   Name: dale   Rate: ??   Phone: ??
ID: 3   Name: ????   Rate: ??   Phone: ??

You need to test the number of fields already there. Here's the script (fields are tab-separated):

/ID/{
s/ID: .* Name: .* Rate: .*/&   Phone: ??/p
t
s/ID: .* Name: .*/&   Rate: ??   Phone: ??/p
t
s/ID: .*/&   Name: ??     Rate: ??   Phone: ??/p
}
w

[address1[,address2]]w file

Append contents of pattern space to file. This action occurs when the command is encountered, rather than when the pattern space is output. Exactly one space must separate the w and the filename. This command will create the file if it does not exist; if the file does exist, its contents will be overwritten each time the script is executed. Multiple write commands that direct output to the same file append to the end of the file.

Example

# Store tbl and eqn blocks in a file:
/^\.TS/,/^\.TE/w troff_stuff
/^\.EQ/,/^\.EN/w troff_stuff
x

[address1[,address2]]x

Exchange contents of the pattern space with contents of the hold space. See h for an example.

y

[address1[,address2]]y/abc/xyz/

Translate characters. Change every instance of a to x, b to y, c to z, etc.

Example

# Change item 1, 2, 3 to Item A, B, C ...
/^item [1-9]/y/123456789/ABCDEFGHI/

f



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.