4.7. sed Examples

% cat datafile
					northwest    NW   Charles Main       3.0 .98 3 34
					western      WE   Sharon Gray        5.3 .97 5 23
					southwest    SW   Lewis Dalsass      2.7 .8  2 18
					southern     SO   Suan Chin          5.1 .95 4 15
					southeast    SE   Patricia Hemenway  4.0 .7  4 17
					eastern      EA   TB Savage          4.4 .84 5 20
					northeast    NE   AM Main Jr.        5.1 .94 3 13
					north        NO   Margot Weber       4.5 .89 5  9
					central      CT   Ann Stephens       5.7 .94 5 13
				

4.7.1. Printing: The p Command (and the --quiet option)

Example 4.5.
% sed '/north/p' datafile
						northwest        NW       Charles Main        3.0  .98  3  34
						northwest        NW       Charles Main        3.0  .98  3  34
						western          WE       Sharon Gray         5.3  .97  5  23
						southwest        SW       Lewis Dalsass       2.7  .8   2  18
						southern         SO       Suan Chin           5.1  .95  4  15
						southeast        SE       Patricia Hemenway   4.0  .7   4  17
						eastern          EA       TB Savage           4.4  .84  5  20
						northeast        NE       AM Main Jr.         5.1  .94  3  13
						northeast        NE       AM Main Jr.         5.1  .94  3  13
						north            NO       Margot Weber        4.5  .89  5   9
						north            NO       Margot Weber        4.5  .89  5   9
						central          CT       Ann Stephens        5.7  .94  5  13
					

Explanation

Prints all lines to standard output by default. If the pattern north is found, sed will print that line in addition to all the other lines.

Example 4.6.
1 % sed –n '/north/p' datafile
						northwest     NW        Charles Main      3.0  .98  3  34
						northeast     NE        AM Main Jr.       5.1  .94  3  13
						north         NO        Margot Weber      4.5  .89  5   9

2 % sed --quiet '/north/p' datafile
						northwest     NW        Charles Main      3.0  .98  3  34
						northeast     NE        AM Main Jr.       5.1  .94  3  13
						north         NO        Margot Weber      4.5  .89  5   9
					

Explanation

  1. The -n option suppresses the default behavior of sed when used with the p command. Without the -n option, sed will print duplicate lines of output as shown in the preceding example. Only the lines containing the pattern north are printed when -n is used.

  2. The --quiet option does the same the as -n.

% cat datafile
						northwest    NW   Charles Main      3.0  .98  3  34
						western      WE   Sharon Gray       5.3  .97  5  23
						southwest    SW   Lewis Dalsass     2.7  .8   2  18
						southern     SO   Suan Chin         5.1  .95  4  15
						southeast    SE   Patricia Hemenway 4.0  .7   4  17
						eastern      EA   TB Savage         4.4  .84  5  20
						northeast    NE   AM Main Jr.       5.1  .94  3  13
						north        NO   Margot Weber      4.5  .89  5   9
						central      CT   Ann Stephens      5.7  .94  5  13
					

4.7.2. Deleting: The d Command

Example 4.7.
% sed '3d' datafile
						northwest        NW    Charles Main       3.0  .98  3  34
						western          WE    Sharon Gray        5.3  .97  5  23
						southern         SO    Suan Chin          5.1  .95  4  15
						southeast        SE    Patricia Hemenway  4.0  .7   4  17
						eastern          EA    TB Savage          4.4  .84  5  20
						northeast        NE    AM Main Jr.        5.1  .94  3  13
						north            NO    Margot Weber       4.5  .89  5   9
						central          CT    Ann Stephens       5.7  .94  5  13
					

Explanation

Deletes the third line. All other lines are printed to the screen by default.

Example 4.8.
% sed '3,$d' datafile
						northwest         NW    Charles Main     3.0  .98  3  34
						western           WE    Sharon Gray      5.3  .97  5  23
					

Explanation

The third line through the last line are deleted. The remaining lines are printed. The dollar sign ($) represents the last line of the file. The comma is called the range operator. In this example, the range of addresses starts at line 3 and ends at the last line, which is represented by the dollar sign ($).

Example 4.9.
% sed '$d' datafile
						northwest        NW     Charles Main      3.0  .98  3  34
						western          WE     Sharon Gray       5.3  .97  5  23
						southwest        SW     Lewis Dalsass     2.7  .8   2  18
						southern         SO     Suan hin          5.1  .95  4  15
						southeast        SE     Patricia Hemenway 4.0  .7   4  17
						eastern          EA     TB Savage         4.4  .84  5  20
						northeast        NE     AM Main Jr.       5.1  .94  3  13
						north            NO     Margot Weber      4.5  .89  5   9
					

Explanation

Deletes the last line. The dollar sign ($) represents the last line. The default is to print all of the lines except those affected by the d command.

Example 4.10.
% sed '/north/d' datafile
						western          WE       Sharon Gray       5.3  .97  5  23
						southwest        SW       Lewis Dalsass     2.7  .8   2  18
						southern         SO       Suan Chin         5.1  .95  4  15
						southeast        SE       Patricia Hemenway 4.0  .7   4  17
						eastern          EA       TB Savage         4.4  .84  5  20
						central          CT       Ann Stevens       5.7  .94  5  13
					

Explanation

All lines containing the pattern north are deleted. The remaining lines are printed.

4.7.3. Substitution: The s Command

Example 4.11.
% sed 's/west/north/g' datafile
						northnorth       NW       Charles Main      3.0  .98  3  34
						northern         WE       Sharon Gray       5.3  .97  5  23
						southnorth       SW       Lewis Dalsass     2.7  .8   2  18
						southern         SO       Suan Chin         5.1  .95  4  15
						southeast        SE       Patricia Hemenway 4.0  .7   4  17
						eastern          EA       TB Savage         4.4  .84  5  20
						northeast        NE       AM Main Jr.       5.1  .94  3  13
						north            NO       Margot Weber      4.5   89  5   9
						central          CT       Ann Stephens      5.7  .94  5  13
					

Explanation

The s command is for substitution. The g flag at the end of the command indicates that the substitution is global across the line; that is, if multiple occurrences of west are found, all of them will be replaced with north. Without the g command, only the first occurrence of west on each line would be replaced with north.

% cat datafile
						northwest    NW    Charles Main      3.0  .98  3  34
						western      WE    Sharon Gray       5.3  .97  5  23
						southwest    SW    Lewis Dalsass     2.7  .8   2  18
						southern     SO    Suan Chin         5.1  .95  4  15
						southeast    SE    Patricia Hemenway 4.0  .7   4  17
						eastern      EA    TB Savage         4.4  .84  5  20
						northeast    NE    AM Main Jr.       5.1  .94  3  13
						north        NO    Margot Weber      4.5  .89  5   9
						central      CT    Ann Stephens      5.7  .94  5  13
					

Example 4.12.
% sed –n 's/^west/north/p' datafile
						northern         WE       Sharon Gray         5.3  .97  5  23
					

Explanation

The s command is for substitution. The -n option with the p flag at the end of the command tells sed to print only those lines where the substitution occurred; that is, if west is found at the beginning of the line and is replaced with north, just those lines are printed.

Example 4.13.
% sed 's/[0–9][0–9]$/&.5/' datafile
						northwest        NW       Charles Main      3.0  .98  3  34.5
						western          WE       Sharon Gray       5.3  .97  5  23.5
						southwest        SW       Lewis Dalsass     2.7  .8   2  18.5
						southern         SO       Suan Chin         5.1  .95  4  15.5
						southeast        SE       Patricia Hemenway 4.0  .7   4  17.5
						eastern          EA       TB Savage         4.4  .84  5  20.5
						northeast        NE       AM Main Jr.       5.1  .94  3  13.5
						north            NO       Margot Weber      4.5  .89  5   9
						central          CT       Ann Stephens      5.7  .94  5  13.5
					

Explanation

The ampersand [a] (&) in the replacement string represents exactly what was found in the search string. Each line that ends in two digits will be replaced by itself, and .5 will be appended to it.

[a] To represent a literal ampersand in the replacement string, it must be escaped, &

Example 4.14.
% sed –n 's/Hemenway/Jones/gp' datafile
						southeast        SE       Patricia Jones     4.0  .7  4  17
					

Explanation

All occurrences of Hemenway are replaced with Jones, and only the lines that changed are printed. The -n option combined with the p command suppresses the default output. The g stands for global substitution across the line.

Example 4.15.
% sed -n 's/(Mar)got/1ianne/p' datafile
						north            NO       Marianne Weber      4.5  .89  5  9
					

Explanation

The pattern Mar is enclosed in parentheses and saved as tag 1 in a special register. It will be referenced in the replacement string as 1. Margot is then replaced with Marianne.

Example 4.16.
% sed 's#3#88#g' datafile
						northwest        NW       Charles Main     88.0   .98  88  884
						western          WE       Sharon Gray       5.88  .97   5  288
						southwest        SW       Lewis Dalsass     2.7   .8    2   18
						southern         SO       Suan Chin         5.1   .95   4   15
						southeast        SE       Patricia Hemenway 4.0   .7    4   17
						eastern          EA       TB Savage         4.4   .84   5   20
						northeast        NE       AM Main Jr.       5.1   .94  88  188
						north            NO       Margot Weber      4.5   .89   5    9
						central          CT       Ann Stephens      5.7   .94   5  188
					

Explanation

The character after the s command is the delimiter between the search string and the replacement string. The delimiter character is a forward slash by default, but can be changed (only when the s command is used). Whatever character follows the s command is the new string delimiter. This technique can be useful when searching for patterns containing a forward slash, such as pathnames or birthdays.

% cat datafile
						northwest    NW    Charles Main      3.0  .98  3  34
						western      WE    Sharon Gray       5.3  .97  5  23
						southwest    SW    Lewis Dalsass     2.7  .8   2  18
						southern     SO    Suan Chin         5.1  .95  4  15
						southeast    SE    Patricia Hemenway 4.0  .7   4  17
						eastern      EA    TB Savage         4.4  .84  5  20
						northeast    NE    AM Main Jr.       5.1  .94  3  13
						north        NO    Margot Weber      4.5  .89  5   9
						central      CT    Ann Stephens      5.7  .94  5  13
					

4.7.4. Range of Selected Lines: The Comma

Example 4.17.

Explanation

All lines in the range of patterns between west and east are printed. If west were to appear on a line after east, the lines from west to the next east or to the end of file, whichever comes first, would be printed. The arrows mark the range.

Example 4.18.
% sed -n '5,/^northeast/p' datafile
						southeast        SE       Patricia Hemenway    4.0  .7   4  17
						eastern          EA       TB Savage            4.4  .84  5  20
						northeast        NE       AM Main Jr.          5.1  .94  3  13
					

Explanation

Prints the lines from line 5 through the first line that begins with northeast.

Example 4.19.

Explanation

For lines in the range between the patterns east and west, the end of line ($) is replaced with the string **VACA**. The newline is moved over to the end of the new string. The arrows mark the range.

4.7.5. Multiple Edits: The e Command

Example 4.20.
% sed –e '1,3d'  –e 's/Hemenway/Jones/' datafile
						southern          SO       Suan Chin          5.1  .95  4  15
						southeast         SE       Patricia Jones     4.0  .7   4  17
						eastern           EA       TB Savage          4.4  .84  5  20
						northeast         NE       AM Main            5.1  .94  3  13
						north             NO       Margot Weber       4.5  .89  5   9
						central           CT       Ann Stephens       5.7  .94  5  13
					

Explanation

The -e option allows multiple edits. The first edit removes lines 1 through 3. The second edit substitutes Hemenway with Jones. Because both edits are done on a per-line basis (i.e., both commands are executed on the current line in the pattern space), the order of the edits may affect the outcome differently. For example, if both commands had performed substitutions on the line, the first substitution could affect the second substitution.

Example 4.21.
% sed --expression='s/TB/Tobias/' --expression='/north/d' datafile
						western      WE    Sharon Gray        5.3     .97     5       23
						southwest    SW    Lewis Dalsass      2.7     .8      2       18
						southern     SO    Suan Chin          5.1     .95     4       15
						southeast    SE    Patricia Hemenway  4.0     .7      4       15
						eastern      EA    Tobias Savage      4.4     .84     5       20
						central      CT    Ann Stephens       5.7     .94     5       13
					

Explanation

The -e option allows multiple edits. A more descriptive option for -e is --expression assigned the value of the sed expression. The first edit tells sed to substitute the regular expression, TB, with Tobias, and the second expression removes all lines containing the pattern north.

% cat datafile
						northwest    NW   Charles Main       3.0  .98  3  34
						western      WE   Sharon Gray        5.3  .97  5  23
						southwest    SW   Lewis Dalsass      2.7  .8   2  18
						southern     SO   Suan Chin          5.1  .95  4  15
						southeast    SE   Patricia Hemenway  4.0  .7   4  17
						eastern      EA   TB Savage          4.4  .84  5  20
						northeast    NE   AM Main Jr.        5.1  .94  3  13
						north        NO   Margot Weber       4.5  .89  5   9
						central      CT   Ann Stephens       5.7  .94  5  13
					

4.7.6. Reading from Files: The r Command

Example 4.22.
% cat newfile
                  ----------------------------------
                  | ***SUAN HAS LEFT THE COMPANY*** |
                  |_________________________________|
% sed '/Suan/r  newfile' datafile
						northwest         NW       Charles Main       3.0  .98  3  34
						western           WE       Sharon Gray        5.3  .97  5  23
						southwest         SW       Lewis Dalsass      2.7  .8   2  18
						southern          SO       Suan Chin          5.1  .95  4  15
                            ----------------------------------
                            |***SUAN HAS LEFT THE COMPANY***   |
                            |__________________________________|
southeast         SE       Patricia Hemenway  4.0  .7   4  17
						eastern           EA       TB Savage          4.4  .84  5  20
						northeast         NE       AM Main            5.1  .94  3  13
						north             NO       Margot Weber       4.5  .89  5   9
						central           CT       Ann Stephens       5.7  .94  5  13
					

Explanation

The r command reads specified lines from a file. The contents of newfile are read into the input file datafile, after the line where the pattern Suan is matched. If Suan had appeared on more than one line, the contents of newfile would have been read in under each occurrence.

4.7.7. Writing to Files: The w Command

Example 4.23.
% sed –n '/north/w newfile2' datafile
						cat newfile2
						northwest        NW       Charles Main       3.0  .98  3  34
						northeast        NE       AM Main Jr.        5.1  .94  3  13
						north            NO       Margot Weber       4.5  .89  5   9
					

Explanation

The w command writes specified lines to a file. All lines containing the pattern north are written to a file called newfile2.

4.7.8. Appending: The a Command

Example 4.24.
% sed '/^north /a\
						--->THE NORTH SALES DISTRICT HAS MOVED<---' datafile
						northwest        NW       Charles Main       3.0  .98  3  34
						western          WE       Sharon Gray        5.3  .97  5  23
						southwest        SW       Lewis Dalsass      2.7  .8   2  18
						southern         SO       Suan Chin          5.1  .95  4  15
						southeast        SE       Patricia Hemenway  4.0  .7   4  17
						eastern          EA       TB Savage          4.4  .84  5  20
						northeast        NE       AM Main Jr.        5.1  .94  3  13
						north            NO       Margot Weber       4.5  .89  5   9
-->THE NORTH SALES DISTRICT HAS MOVED<---
central          CT       Ann Stephens       5.7  .94  5  13
					

Explanation

The a command is the append command. The string ---> The NORTH SALES DISTRICT HAS MOVED<--- is appended after lines beginning with the pattern north, when north is followed by a space. The text that will be appended must be on the line following the append command.

Sed requires a backslash after the a command. The second backslash is used by the TC shell to escape the newline so that its closing quote can be on the next line.[a] If more than one line is appended, each line, except the last one, must also end in a backslash.

[a] The Bash, Bourne, and Korn shells do not require the second backslash to escape the newline, because they do not require quotes to be matched on the same line, only that they match.

% cat datafile
						northwest    NW   Charles Main       3.0  .98  3  34
						western      WE   Sharon Gray        5.3  .97  5  23
						southwest    SW   Lewis Dalsass      2.7  .8   2  18
						southern     SO   Suan Chin          5.1  .95  4  15
						southeast    SE   Patricia Hemenway  4.0  .7   4  17
						eastern      EA   TB Savage          4.4  .84  5  20
						northeast    NE   AM Main Jr.        5.1  .94  3  13
						north        NO   Margot Weber       4.5  .89  5   9
						central      CT   Ann Stephens       5.7  .94  5  13
					

4.7.9. Inserting: The i Command

Example 4.25.
% sed '/eastern/i\
						NEW ENGLAND REGION\
						-------------------------------------' datafile
						northwest        NW       Charles Main       3.0  .98  3  34
						western          WE       Sharon Gray        5.3  .97  5  23
						southwest        SW       Lewis Dalsass      2.7  .8   2  18
						southern         SO       Suan Chin          5.1  .95  4  15
						southeast        SE       Patricia Hemenway  4.0  .7   4  17
						NEW ENGLAND REGION
---------------------------------
eastern          EA       TB Savage          4.4  .84  5  20
						northeast        NE       AM Main Jr.        5.1  .94  3  13
						north            NO       Margot Weber       4.5  .89  5   9
						central          CT       Ann Stephens       5.7  .94  5  13
					

Explanation

The i command is the insert command.If the pattern eastern is matched, the i command causes the text following the backslash to be inserted above the line containing eastern. A backslash is required after each line to be inserted, except the last one. (The extra backslash is for the TC shell.)

4.7.10. Next: The n Command

Example 4.26.

Explanation

If the pattern eastern is matched on a line, the n command causes sed to get the next line of input (the line with AM Main Jr.), replace the pattern space with this line, substitute (s AM with Archie, print the line, and continue.

4.7.11. Transform: The y Command

Example 4.27.

Explanation

For lines 1 through 3, the y command translates all lowercase letters to uppercase letters. Regular expression metacharacters do not work with this command.

% cat datafile
						northwest    NW   Charles Main      3.0  .98  3  34
						western      WE   Sharon Gray       5.3  .97  5  23
						southwest    SW   Lewis Dalsass     2.7  .8   2  18
						southern     SO   Suan Chin         5.1  .95  4  15
						southeast    SE   Patricia Hemenway 4.0  .7   4  17
						eastern      EA   TB Savage         4.4  .84  5  20
						northeast    NE   AM Main Jr.       5.1  .94  3  13
						north        NO   Margot Weber      4.5  .89  5   9
						central      CT   Ann Stephens      5.7  .94  5  13
					

4.7.12. Quit: The q Command

Example 4.28.
% sed '5q' datafile
						northwest        NW  Charles Main      3.0  .98  3  34
						western          WE  Sharon Gray       5.3  .97  5  23
						southwest        SW  Lewis Dalsass     2.7  .8   2  18
						southern         SO  Suan Chin         5.1  .95  4  15
						southeast        SE  Patricia Hemenway .0   .7   4  17
					

Explanation

After the line 5 is printed, the q command causes the sed program to quit.

Example 4.29.
% sed '/Lewis/{ s/Lewis/Joseph/;q; }' datafile
						northwest       NW       Charles Main       3.0  .98  3  34
						western         WE       Sharon Gray        5.3  .97  5  23
						southwest       SW       Joseph Dalsass     2.7  .8   2  18
					

Explanation

When the pattern Lewis is matched on a line, the substitution command (s) first replaces Lewis with Joseph, and then the q command causes the sed program to quit.

4.7.13. Holding and Getting: The h and g Commands

Example 4.30.

Explanation

As sed processes the file, each line is stored in a temporary buffer called the pattern space. Unless the line is deleted or suppressed from printing, the line will be printed to the screen after it is processed. The pattern space is then cleared and the next line of input is stored there for processing. In this example, after the line containing the pattern northeast is found, it is placed in the pattern space and the h command copies it and places it in another special buffer called the holding buffer. In the second sed instruction, when the last line is reached ($) the G command tells sed to get the line from the holding buffer and put it back in the pattern space buffer, appending it to the line that is currently stored there—in this case, the last line. Simply stated: Any line containing the pattern northeast will be copied and appended to the end of the file. (See Figure 4.1.)

Figure 4.1. The pattern space and holding buffer. See Example 4.33.


% cat datafile
						northwest    NW  Charles Main      3.0  .98  3  34
						western      WE  Sharon Gray       5.3  .97  5  23
						southwest    SW  Lewis Dalsass     2.7  .8   2  18
						southern     SO  Suan Chin         5.1  .95  4  15
						southeast    SE  Patricia Hemenway 4.0  .7   4  17
						eastern      EA  TB Savage         4.4  .84  5  20
						northeast    NE  AM Main Jr.       5.1  .94  3  13
						north        NO  Margot Weber      4.5  .89  5   9
						central      CT  Ann Stephens      5.7  .94  5  13
					

Example 4.31.

Explanation

If the pattern WE is found on a line, the h command causes the line to be copied from the pattern space into a holding buffer. When stored in the holding buffer, the line can be retrieved (G or g command) at a later time. In this example, when the pattern WE is found, the line where it was found is stored in the pattern buffer first. The h command then puts a copy of the line in the holding buffer. The d command deletes the copy in the pattern buffer. The second command searches for CT in a line, and when it is found, sed gets (G) the line that was stored in the holding buffer and appends it to the line currently in the pattern space. Simply stated: The line containing WE is moved and appended after the line containing CT. (See "Holding and Exchanging: The h and x Commands" on page 117.)

Example 4.32.

Explanation

As sed processes the file, each line is stored in a temporary buffer called the pattern space. Unless the line is deleted or suppressed from printing, the line will be printed to the screen after it is processed. The pattern space is then cleared and the next line of input is stored there for processing. In this example, after the line containing the pattern northeast is found, it is placed in the pattern space. The h command takes a copy of it and places it in another special buffer called the holding buffer. In the second sed instruction, when the last line is reached ($), the g command tells sed to get the line from the holding buffer and put it back in the pattern space buffer, replacing the line that is currently stored there—in this case, the last line. Simply stated: The line containing the pattern northeast is copied. The copy overwrites the last line in the file.

% cat datafile
						northwest    NW  Charles Main      3.0  .98  3  34
						western      WE  Sharon Gray       5.3  .97  5  23
						southwest    SW  Lewis Dalsass     2.7  .8   2  18
						southern     SO  Suan Chin         5.1  .95  4  15
						southeast    SE  Patricia Hemenway 4.0  .7   4  17
						eastern      EA  TB Savage         4.4  .84  5  20
						northeast    NE  AM Main Jr.       5.1  .94  3  13
						north        NO  Margot Weber      4.5  .89  5   9
						central      CT  Ann Stephens      5.7  .94  5  13
					

Example 4.33.
% sed -e '/WE/{h; d; }' -e '/CT/{g; }'datafile
						northwest        NW       Charles Main      3.0  .98  3  34
						southwest        SW       Lewis Dalsass     2.7  .8   2  18
						southern         SO       Suan Chin         5.1  .95  4  15
						southeast        SE       Patricia Hemenway 4.0  .7   4  17
						eastern          EA       TB Savage         4.4  .84  5  20
						northeast        NE       AM Main Jr.       5.1  .94  3  13
						north            NO       Margot Weber      4.5  .89  5   9
						western          WE       Sharon Gray       5.3  .97  5  23
					

Explanation

If the pattern WE is found, the h command copies the line into the holding buffer; the d command deletes the line in the pattern space. When the pattern CT is found, the g command gets the copy in the holding buffer and overwrites the line currently in the pattern space. Simply stated: Any line containing the pattern WE will be moved to overwrite lines containing CT. (See Figure 4.1 on page114.)

4.7.14. Holding and Exchanging: The h and x Commands

Example 4.34.
% sed -e  '/Patricia/h' -e '/Margot/x' datafile
						northwest         NW      Charles Main       3.0  .98  3  34
						western           WE      Sharon Gray        5.3  .97  5  23
						southwest         SW      Lewis Dalsass      2.7  .8   2  18
						southern          SO      Suan Chin          5.1  .95  4  15
						southeast         SE      Patricia Hemenway  4.0  .7   4  17
						eastern           EA      TB Savage          4.4  .84  5  20
						northeast         NE      AM Main Jr.        5.1  .94  3  13
						southeast         SE      Patricia Hemenway  4.0  .7   4  17
						central           CT      Ann Stephens       5.7  .94  5  13
					

Explanation

The x command exchanges (swaps) the contents of the holding buffer with the current pattern space. When the line containing the pattern Patricia is found, it will be stored in the holding buffer. When the line containing Margot is found, the pattern space will be exchanged for the line in the holding buffer. Simply stated: The line containing Margot will be replaced with the line containing Patricia.

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

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