DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Manipulating text with sed

Whole-line oriented functions

These functions are used to manipulate an entire line at a time in the pattern space, rather than a string within a line. The following whole-line oriented functions are available:


d
Deletes from the file all lines matched by its addresses. No further functions are executed on a deleted line. As soon as the d function is executed, a new line is read from the input, and the list of editing commands is restarted from the beginning on the new line. The maximum number of addresses is two. For example, /charlie/d deletes all lines containing the string ``charlie''.

n
Reads the next line of input into the pattern space. The current line is sent to the standard output, if appropriate, and the line counter is incremented by one; the execution of editing commands continues following the n function rather than looping back up to the top of the command list (as it would if sed had naturally exhausted its command list on the current pattern space, and read a new input line). The maximum number of addresses is two.
The following three commands must be specified over multiple lines and the text must appear on a new line. Interior newlines must be hidden by a backslash character (\) immediately preceding each newline. The text argument is terminated by the first unhidden newline, which is the first one not immediately preceded by backslash. Once a function executes successfully, the text is written to the output regardless of what later commands do to the line that triggered it, even if the line is subsequently deleted. The text is not scanned for address matches, and no editing commands are attempted on it, nor does it cause any change in the line number counter.

a
Appends text. The text following the a function is appended when the pattern space is sent to the standard output. For example, the following sequence appends the line ``(Not to mention blue)'' after any line containing the word ``red'':

/red/a\
(Not to mention blue)

The a command has only one possible address.


i
Inserts text. When followed by a text argument, i functions the same as a, except that the text is written to the output before the matched line. It has only one possible address. The following sequence inserts the line ``(Not to mention blue)'' before any line containing the word ``red'':

/red/i\
(Not to mention blue)


c
Changes text. The c function deletes the lines selected by its addresses, and replaces them with the lines in the text. (This function is principally used for replacing an entire line with a different line, not for routine find and replace operations (for which the s, substitute, function is used.) The following example searches for lines containing the word ``secret'', deletes them, and replaces them with the text ``[this line has been censored]'':

/secret/c\
[this line has been censored]

The c function may have two addresses, and therefore select a range of lines. If it does, all the lines in the range are deleted, but only one copy of the text is written to the output, not one copy per line deleted. After a line has been deleted by a c function, no further commands are attempted on it. If text is appended after a line by an a function, and the line is subsequently changed, the text inserted by the c function is placed before the text of the a function.

As with all sed commands, these three multiline commands may be used either in scripts or on the shell command line. In the latter case, the first line's trailing backslash must be quoted, as follows:

$ sed -e "/secret/c\\
> [this line has been censored]" input_file


Next topic: Substitute functions
Previous topic: Functions

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 03 June 2005