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

Input-output functions

These functions enable sed scripts to read and write files directly:


p
The print function writes the contents of the pattern space (that is, any lines matching the desired address) to the standard output file at the time sed encounters the p function, regardless of what succeeding editing commands do to the lines. The maximum number of possible addresses is two.

wfile
The write function appends the addressed lines to file. If the file previously existed, it is overwritten; if not, it is created. The lines are written exactly as they exist when the write function is encountered for each line, regardless of what subsequent editing commands do to them. Exactly one space must separate the w function and the filename. The combined number of write functions and w flags may not exceed 10.

r
The read function reads the contents of the named file, and appends them after the line matched by the address. The file is read and appended regardless of what subsequent editing commands do to the line that matched its address. If r and a functions are executed on the same line, the text from the a functions and the r functions is written to the output in the order that the functions are executed. Exactly one space must separate the r and the filename; only one address is possible. If a file mentioned by an r function cannot be opened, it is considered a null file rather than an error, and no diagnostic is given.
The p and w functions perform the same operations as the corresponding substitution flags (see ``Substitute functions'') with the exception that they are not dependent on a successful substitution.

You may have up to 20 appends in one script, and 10 w files open. These limits are built into sed.

In the example below, the file support.extensions contains the following list of telephone extensions:

   Fred:	x5706
   Marge:	x5631
   Sally:	x5239
The command /phone list:/r support.extensions can be applied to a file like the following:
   Thank you for enquiring about our customer support hotline.
   Please place your calls to our exchange at 346-4573, then
   dial one of the following extensions:
   

phone list:

The command has the following effect:
   Thank you for enquiring about our customer support hotline.
   Please place your calls to our exchange at 346-4573, then
   dial one of the following extensions:
   

phone list: Fred: x5706 Marge: x5631 Sally: x5239

In this way, sed can be used to automatically insert smaller files into the file currently being edited.

Note, however, that the line matching the context address (in the example, the line consisting of the string ``phone list:'') must be terminated with a newline.


Next topic: Multiple input-line functions
Previous topic: The transform function

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003