sed—Stream Editor

sed is a stream editor. It is a noninteractive text editor, which enables you to edit a text file directly from the command line. Editing a file with sed takes place one line at a time, and because of this, the entire file does not need to be stored in a buffer. Because the file does not need to be stored in a buffer, there is no limit on the size of the file that can be edited. This is a good tool for editing files that are too large for the other editors.

The stream editor does not operate in an interactive mode like most of the other editors. Modifications that are made can only be reviewed after the editing has been performed. The command syntax for sed is as follows:

sed 'address/pattern/   action'  filename 

The sed command is followed by an address (optional), a pattern, and the action to perform when a match is found. Notice that the address , pattern , and action are enclosed in single quotes. The name of the input file is the last argument.

Although the default mode of sed is to apply edit commands globally on the file, a number of methods can be used to designate which lines (address range) in the file are to be edited. Lines can be designated by the following:

  • A line number (address)

  • A range of line numbers (address range)

  • A matching pattern

  • A matching pattern on a specific line number

  • A matching pattern within a range of line numbers

sed has regular expression capabilities, making most of the editing operations in sed fairly complicated. I won’t attempt to explain all of the intricacies of sed, but I will describe how to perform some useful operations using this powerful editor.

sed Instructions

There is a complete set of editing instructions for the sed command, including the following:

d Delete the specified lines.
-n,p The –n option, used with the print instruction (p), prints only the modified lines. The default is to print all input lines to the screen, modified and unmodified.
s Substitute the first occurrence in a line.
s,g Substitute globally, for each occurrence in the line.

The address, or range, is placed at the beginning of the sed command, directly after the first single quote. In the following example, I’ll specify a single line to be deleted in file1:

sed '3d' file1 

In the next example, I’ll delete a range of lines, 1 through 6, from file1:

sed '1,6d' file1 

As I instruct sed to delete these lines, a key point to remember is that the lines are not really being removed from the original file; they are only being removed from the output. Currently, my standard output is the screen. To make changes permanent, redirect the output to a file as follows:

sed '1,6d' file1 > file2 

Pattern Searching

To search for a text string, or pattern, in all lines of the file, do not include an address. The sed command used to search an entire file for a particular pattern, in this case SunOS, is as follows:

sed –n '/SunOS/p'  file1 

The preceding example will search a file named file1 for the text string SunOS. The results will be printed to the screen. I used the –n option to print only those lines that contain the pattern match. If the –n and p combination is not used, all lines in the file will be printed.

Substitution

The format for substitution is as follows:

sed 's/SunOS/Solaris/' file1 

In the preceding example, Solaris will replace the first occurrence of SunOS in each line. If SunOS appears more than once in the line, only the first occurrence will be replaced.

To replace all occurrences in a line, add the g (global) instruction as follows:

sed 's/SunOS/Solaris/g' file1 

The g instruction at the end of the substitution specifies a global replacement in which all occurrences of SunOS in a line are replaced.

A line or range can be added to the s instruction to specify where substitutions are to be made in the file, as follows:

sed '2,7s/SunOS/Solaris/g' file1 

In this example, substitutions will only be made on lines 2 through 7.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset