< Day Day Up > |
5.9. sed ExamplesThe following examples show you how to use sed, its options, commands, and regular expressions. Remember that sed is non-destructive; that is the file that sed is editing will not be modified unless its output is redirected, as shown in Example 5.6. The examples in the following sections use the following datafile, repeated periodically for your convenience.
5.9.1 Printing: The p CommandThe p command is the print command. It is used to display the contents of the pattern buffer. Because, by default, sed prints lines to the screen, the n option is used to suppress default printing. With the n option and the p command together, selected text can be printed. Example 5.8.% 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 5.9.% 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 EXPLANATION By default, sed prints the line currently in the pattern buffer. With the p command, sed is told to print the line again. The n option suppresses the default behavior of sed. When used with the p command, the line in the pattern buffer is printed only once. 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. 5.9.2 Deleting: The d CommandThe d command is used to delete lines. After sed copies a line from a file and puts it into a pattern buffer, it executes any sed commands for that line, and finally, displays the contents of the pattern buffer to the screen. When the d command is issued, the line currently in the pattern buffer is removed, not displayed.
Example 5.10.% 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 5.11.% 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 dollar sign ($) represents the last line of the file. The comma is called the range operator. The remaining lines are printed. 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 5.12.% 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 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 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 5.13.% 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 Stephens 5.7 .94 5 13 EXPLANATION All lines containing the pattern north are deleted. The remaining lines are printed. 5.9.3 Substitution: The s CommandThe s command is the substitution command. Substitution and replacement of text in files is accomplished with sed's s command. The text enclosed in the forward slashes after the s command is a regular expression, followed by the replacement text on the right-hand side. Replacements can be done globally across a line if the g flag is used.
Example 5.14.% 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. Example 5.15.% 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 5.16.% sed 's/[09][09]$/&.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.
Example 5.17.% 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 5.18.% 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 5.19.% 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. Whatever character follows the s command (with the exception of the newline character or backslash) is the new delimiter. This technique can be useful when searching for patterns containing a forward slash, such as pathnames or birthdays. 5.9.4 Range of Selected Lines: The CommaA range of lines starts at one address in a file and ends at another address in the file. The address range can be line numbers (such as 5,10), regular expressions (such as /Dick/,/Joe/), or a combination of both (such as /north/,$). The range is inclusiveit consists of the line with the starting condition, all lines in between, and the line ending the condition of the range. If the ending condition is never reached, it ends when the file ends. If the ending condition is reached, and then the condition for starting again applies, the range starts over. Example 5.20.
% sed n '/west/,/east/p' datafile
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 5.21.% 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 5.22.% sed '/west/,/east/s/$/**VACA**/' datafile 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 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. 5.9.5 Multiple Edits: The e CommandThe e command is the edit command, and is used if you have more than one editing command for sed to execute. All edits are applied to the line in the pattern buffer, before the next line is edited.
Example 5.23.% 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 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 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. For example, if both commands had performed substitutions on the line, the first substitution could affect the second substitution. 5.9.6 Reading from Files: The r CommandThe r command is the read command. It allows sed to load content from one text file into the current file at some specified location. Example 5.24.% 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 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 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. 5.9.7 Writing to Files: The w CommandThe w command is the write command. It allows sed to write lines from the current file ito another file. Example 5.25.% sed n '/north/w newfile' datafile cat newfile 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 newfile. 5.9.8 Appending: The a CommandThe a command is the append command. Appending causes new text to be placed below the current line in the file; that is, the line that was just read into the pattern buffer. Whether at the command line or in a sed script, the a command is followed by a backslash. If you are at the C/TC command line, two backslashes are required as shown in Example 5.26one backslash is for sed's append command, and one is for line continuation. With C/TC the opening quote that starts the sed command must be terminated on the same line. The second backslash makes it possible to continue typing on the next line without an "Unmatched quote" error. (The Bourne and Bash shells do not require the second backslash because closing quotes do not have to be matched on the same line.) The line of text that will be appended is placed after the sed command on its own line. If additional lines are to be appended, each line must be terminated with a backslash, except the final line, which ends with the closing quote and filename. That line will not be terminated with a backslash.
Example 5.26.% sed '/^north /a\\ # Use two backslashes only if the shell is C/TC --->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 C 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 with a backslash.
5.9.9 Inserting: The i CommandThe i command is the insert command. It is like the a command, but instead of placing text below the current line, it inserts new text above the current line in the file; that is, the line that was just read into the pattern buffer. Whether at the command line or in a sed script, the i command is followed by a backslash. Example 5.27.% sed '/eastern/i\\ # Use two backslashes for C/TC 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 C shell.) 5.9.10 Changing: The c CommandThe c command is the change command. It allows sed to modify or change existing text with new text. The old text is overwritten with the new.
Example 5.28.% sed '/eastern/c\\ THE EASTERN REGION HAS BEEN TEMPORARILY CLOSED' 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 THE EASTERN REGION HAS BEEN TEMPORARILY CLOSED 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 c command is the change command. It completely changes or modifies the current line in the pattern buffer. If the pattern eastern is matched, the c command causes the text following the backslash to replace the line containing eastern. A backslash is required after each line to be inserted, except the last one. (The extra backslash is for the C shell.) 5.9.11 Next: The n CommandThe n command is the next command. It causes sed to get the next line from the input file, and read it into the pattern buffer. Any sed commands will be applied to the line immediately following the line that is being addressed. Example 5.29.% sed '/eastern/{ n; s/AM/Archie/; }' 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 Archie 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 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 AM with Archie, print the line, and continue. 5.9.12 Transform: The y CommandThe y command is the transform command. It is similar to the UNIX/Linux tr command. Characters are translated on a one-to-one, corresponding basis from those on the left-hand side to those on the right-hand side. For example, in the command y/abc/ABC/ any lowercase letter a will be transformed to an uppercase A, as will b to B, and c to C. Example 5.30.% sed '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKL MNOPQRSTUVWXYZ/' datafile 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 For lines 1 through 3, the y command translates all lowercase letters to uppercase letters. Regular expression metacharacters do not work with this command, and like the substitution delimiter, the forward slash can be changed to a different character. 5.9.13 Quit: The q CommandThe q command is the quit command. It causes the sed program to exit without further processing.
Example 5.31.% 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 4.0 .7 4 17 EXPLANATION After line 5 is printed, the q command causes the sed program to quit. Example 5.32.% 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, first the substitution command (s) replaces Lewis with Joseph, and then the q command causes the sed program to quit. 5.9.14 Holding and Getting: The h and g CommandsThe h command is for holding; the g command is for getting. When a line is read in from a file, sed places it in the pattern buffer and executes commands on that line. There is an additional buffer called the holding buffer. (Both buffers can hold up to 8,192 bytes.) A line can be sent from the pattern buffer to a holding buffer with the h command, later to be retrieved with the g or G (get) command.
Example 5.33.% sed -e '/northeast/h' -e '$G' 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 northeast NE AM Main Jr. 5.1 .94 3 13 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 into 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. Example 5.34.% 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 central CT Ann Stephens 5.7 .94 5 13 western WE Sharon Gray 5.3 .97 5 23 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, from which the line can be retrieved (G or g command) at a later time. In this example, the line where the pattern WE is 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, so that it will not be displayed. 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 after the line containing CT. See also "Holding and Exchanging: The h and x Commands" on page 151. Example 5.35.% sed -e '/northeast/h' -e '$g' 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 northeast NE AM Main Jr. 5.1 .94 3 13 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 and moved to overwrite the last line in the file.
Example 5.36.% 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 5.1.) Figure 5.1. The pattern space and holding buffer. See Example 5.36.5.9.15 Holding and Exchanging: The h and x CommandsThe x command is the exchange command. It lets sed exchange the pattern in the holding buffer with what is currently in the pattern buffer. Example 5.37.% 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. |
< Day Day Up > |