Essential SED Commands


Essential SED Commands

Now that we have some basic details of sed under our belts, let s look at some of the most useful commands. We ll dig in a little further to these commands to explore some of their other uses.

Substitute ( s )

The substitute command, as we ve explored already, provides a simple search and replacement over the input stream. As we ve already investigated this command, we ll cover some of the other aspects not yet touched upon. The format of the substitute is as follows :

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

An optional address or address range can be specified before the substitute command. A single address indicates that the substitution is restricted to the particular line or search expression. Two addresses or patterns (separated by a comma) indicate a range for the substitution. The flags (shown at the end of the command specification) are n , representing a number, and g , representing global . If a number is specified, the substitution is performed only on that occurrence. For example:

 sed '/this/that/' file.txt 

replaces the first occurrence (of each line) contained in the input file,  file.txt . We could achieve the same thing with the script:

 sed '/this/that/1' file.txt 

If we wanted the second occurrence of this to be replaced with that , we d simply specify n as 2 . The global flag specifies that all occurrences on all inputs lines will be replaced.

Delete ( d )

The delete command simply deletes the lines that match our defined restriction. For example, if we wanted to remove the first five lines of a file, we could use the following sed command:

 sed '1,5d' file.txt 

We could tell sed to delete all but the first five lines using the delete command and reversing the restriction with ! :

 sed '1,5!d' file.txt 

Print ( p )

The print command can be thought of as the reverse of delete. One difference is that we must specify the -n flag to avoid double-printing the output. To reproduce our earlier delete example of emitting only the first five lines:

 sed -n '1,5p' file.txt 

We could also instruct sed to emit only those lines that contain a particular search string, such as those containing a : at the beginning of the line:

 sed -n '/^:/p' file.txt 

Appending ( a ), Inserting ( i ), and Changing ( c ) Lines

We can also append, insert, or change entire lines based upon range or pattern. To insert a blank line after a line is found containing start , we could perform the following append command:

 sed '/start/a\ ' file.txt 

We could insert a blank line at the end of the file using the insert command:

 sed '$i\ ' file.txt 

We can change a line entirely as follows. This script looks for the word secret and replaces the entire line with DELETED :

 sed '/secret/c\DELETED' file4.txt 

Finally, we can perform all three commands using the {} symbols to group the commands. The following script will emit a start line before the line matching the keyword, a stop line after the matching line, and then emit DELETED for the line itself (on the CD-ROM at ./source/ch21/multi.sed ).

 sed '/secret/{     i\     start     a\     end     c\     DELETED     }' file.txt 

The grouping shown here can apply to other commands, but with some restrictions. For example, when the delete command is used, all commands after it in the grouping are ignored because the delete command terminates the editing session.

Quit ( q )

The quit command, as the name implies, simply ends the sed editing session. This command can be quite useful. Consider the following example:

 sed '10q' file.txt 

This command emits the first 10 lines of the  file.txt , and when the tenth line is reached, the sed session is ended. This particular command emulates another useful GNU/Linux command called head (which emits the head of a file).

Transformation ( y )

We can transform text using the y command. This provides for replacing one character for another. We provide two sets of characters : the first refers to the search set and the second the replacement set. For example, if we encounter an A , we replace with an a , and so on.

To convert all uppercase letters to lowercase, we could use the following:

 sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' file 

The pattern and replacement strings represent a one-to-one correspondence for which the transformation will take place. For this reason, each string must be of equal length.

Line Numbering ( = )

The = command is used to emit the current line number. This can be used to emit the line numbers that match a given search string or to simply emit the number of lines in the input file.

For example, if we re interested in which lines contain a given search pattern, we could accomplish this with the following sed script:

 sed -n '/This/=' file.txt 

This script results in line numbers emitted (one per line) for each line that contains the word This .

Holding the Pattern Space ( h )

Using the hold buffer, we can store away the pattern space to the hold buffer and then operate on the pattern buffer directly. The following example illustrates emitting both the altered line and the unaltered line. Also illustrated here are comments within sed scripts (all characters after the # symbol).

 sed '{     # store the pattern space to the hold buffer     h     # perform the substitution on the pattern space      s/is/IS/     # append the unaltered line to the pattern space     G     }' file.txt 

This script will emit the altered line and follow it immediately with the unaltered line (the line not having been altered by the substitute command).




GNU/Linux Application Programming
GNU/Linux Application Programming (Programming Series)
ISBN: 1584505680
EAN: 2147483647
Year: 2006
Pages: 203
Authors: M. Tim Jones

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