Section 10.5. Alphabetical Summary of sed Commands


10.5. Alphabetical Summary of sed Commands

GNU sed lets you use the filenames /dev/stdin, /dev/stdout and /dev/stderr to refer to standard input, output, and error, respectively, for the r, R, w, and W commands and the w flag to the s command.

GNU-specific commands or extensions are noted with {G} in the command synopsis. When the GNU version allows a command to have two addresses, the command is performed for each input line within the range.

#

     # 

Begin a comment in a sed script. Valid only as the first character of the first line. (Some versions, including GNU sed, 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. According to POSIX, sed must support labels that are unique in the first eight characters. GNU sed has no limit, but some older versions support up to only seven characters.

=

     [/pattern/]=     [address1[,address2]]= {G} 

Write to standard output the line number of each line addressed by pattern.

a

     [address]a\     text     [address1[,address2]]a \ {G}          text 

Append text following each line matched by address. If text goes over more than one line, newlines must be "hidden" by preceding them with a backslash. The text is terminated by the first newline that is not hidden 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.

The GNU version accepts two addresses and allows you to put the first line of text on the same line as the a command.

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] 

Unconditionally transfer control 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 HTML tables; resume script after </table>:     /<table/,/<\/table>/b 

c

     [address1[,address2]]c\     text 

Replace (change) the lines selected by the address(es) with text. (See a for details on text.) When a range of lines is specified, all lines are replaced as a group by a single copy of text. The contents of the pattern space are, in effect, deleted, and no subsequent editing commands can be applied to the pattern space (or to text).

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 empty lines, including lines with just     whitespace:     /^[#tab]*$/d 

D

     [address1[,address2]]D 

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

Example

     # Strip multiple blank lines, leaving only one:     /^$/{     N     /^\n$/D     } 

e

     [address1[,address2]]e [command] {G} 

With command, execute the command and send the result to standard output. Without command, execute the contents of the pattern space as a command, and replace the pattern space with the results.

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 that a newline and the hold space are pasted to the end of the pattern space 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 a newline and then the contents of the pattern space 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

     [address]i\     text     [address1[,address2]]i \ {G}          text 

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

The GNU version accepts two addresses and allows you to put the first line of text on the same line as the i command.

Example

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

l

     [address1[,address2]]l     [address1[,address2]]l [len] {G} 

List the contents of the pattern space, showing nonprinting characters as ASCII codes. Long lines are wrapped. With GNU sed, len is the character position at which to wrap long lines. A value of 0 means to never break lines.

n

     [address1[,address2]]n 

Read the 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 DocBook/XML, titles follow section tags. Suppose you are using a convention where each opening section tag is on a line by itself, with the title on the following line. To print all the section titles, invoke this script with sed -n:

     /<sect[1-4]/{     n     p     } 

N

     [address1[,address2]]N 

Append the next input line to contents of pattern space; the new line is separated from the previous contents of the pattern space by a newline. (This command is designed to allow pattern matches across two lines.) By using \n to match the embedded newline, you can match patterns across multiple lines. See the Example under D.

Examples

Like the Example in n, but print the section tag line as well as header title:

     /<sect[1-4]/{     N     p     } 

Join two lines (replace newline with space):

     /<sect[1-4]/{     N     s/\n/ /     p     } 

p

     [address1[,address2]]p 

Print the addressed line(s). Note that this can result in duplicate output unless default output is suppressed by using #n or the -n command-line option. Typically used before commands that change control flow (d, n, b), which might prevent the current line from being output. See the Examples under h, n, and N.

P

     [address1[,address2]]P 

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

Example

Suppose you have function references in two formats:

     function(arg1, arg2)     function(arg1,              arg2) 

The following script changes argument arg2, regardless of whether it appears on the same line as the function name:

     s/function(arg1, arg2)/function(arg1, XX)/     /function(/{     N     s/arg2/XX/     P     D     } 

q

     [address]q     [address]q [value] {G} 

Quit when address is encountered. The addressed line is first written to the output (if default output is not suppressed), along with any text appended to it by previous a or r commands. GNU sed allows you to provide value, which is used as the exit status.

Examples

Delete everything after the addressed line:

     /Garbled text follows:/q 

Print only the first 50 lines of a file:

     50q 

Q

     [address]Q [value] {G} 

Quits processing, but without printing the pattern space. If value is provided, it is used as sed's exit status.

r

     [address]r file     [address1[,address2]]r file {G} 

Read contents of file and append to the output after the contents of the pattern space. There must be exactly one space between the r and the filename. The GNU version accepts two addresses.

Example

     /The list of items follows:/r item_file 

R

     [address1[,address2]]R file {G} 

Read one line of file and append to the output after the contents of the pattern space. Successive R commands read successive lines from 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. Any delimiter may be used. Use \ within pattern or replacement to escape the delimiter. The following flags can be specified (those marked with a | are specific to GNU sed):


n

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


e

If the substitution was made, execute the contents of the pattern space as a shell command and replaces the pattern space with the results.


g

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


i or I

Do a case-insensitive regular expression match.


m or M

Allow ^ and $ to match around a newline embedded in the pattern space.


p

Print the line if the substitution is successful. If several successful substitutions are successful, sed prints multiple copies of the line.


w file

Write the line to file if a replacement was done. In the traditional Unix sed, a maximum of 10 different files can be opened.

GNU sed allows you to use the special filenames /dev/stdout and /dev/stderr to write to standard output or standard error, respectively.

Within the replacement, GNU sed accepts special escape sequences, with the following meanings:


\L

Lowercase the replacement text until a terminating \E or \U.


\l

Lowercase the following character only.


\U

Uppercase the replacement text until a terminating \E or \L.


\u

Uppercase the following character only.


\E

Terminate case conversion from \L or \U.

Examples

Here are some short, commented scripts:

     # Change third and fourth quote to ( and ):     /function/{     s/"/)/4     s/"/(/3     }     # Remove all quotes on a given line:     /Title/s/"//g     # Remove first colon and 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 successful substitutions have been made on addressed lines, and if so, branch to the line marked by :label. (See b and :.) If label is not specified, control branches to the bottom of the script. The t command is like a case statement in the C programming language or the various 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 

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):

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

T

     [address1[,address2]]T [label] {G} 

Like t, but only branches to label if there not any successful substitutions. (see b, t, and :). If label is not specified, control branches to the bottom of the script.

v

     [address1[,address2]]v [version] {G} 

This command doesn't do anything. You use it to require GNU sed for your script. This works, because non-GNU versions of sed don't implement the command at all, and will therefore fail. If you supply a specific version, GNU sed fails if the required version is newer than the one executing the script.

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 exists, 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.

GNU sed allows you to use the special filenames /dev/stdout and /dev/stderr to write to standard output or standard error, respectively.

Example

     # Store HTML tables in  a file     /<table/,/<\/table>/w tables.html 

W

     [address1[,address2]]W file 

Like w, but only writes the contents of the first line in the pattern space to the file.

x

     [address1[,address2]]x 

Exchange the contents of the pattern space with the 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/i123456789/IABCDEFGHI/ 



Linux in a Nutshell
Linux in a Nutshell
ISBN: 0596154488
EAN: 2147483647
Year: 2004
Pages: 147

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net