ITPub博客

首页 > Linux操作系统 > Linux操作系统 > sed的一些例子

sed的一些例子

原创 Linux操作系统 作者:hjgluguo 时间:2009-08-27 00:21:11 0 删除 编辑

文件fruit_prices.txt
Fruit           Price/lbs
Banana          0.89
Paech           0.79
Kiwi            1.50
Pineapple       1.29
Apple           0.99
Mango           2.20

[root@oracle9i shell]# sed '/0\.[0-9][0-9]$/p ' fruit_prices.txt
Fruit           Price/lbs
Banana          0.89
Banana          0.89
Paech           0.79
Paech           0.79
Kiwi            1.50
Pineapple       1.29
Apple           0.99
Apple           0.99

[root@oracle9i shell]# sed -n '/0\.[0-9][0-9]$/p ' fruit_prices.txt
Banana          0.89
Paech           0.79
Apple           0.99

[root@oracle9i shell]# sed '/^[Mm]ango/d' fruit_prices.txt
Fruit           Price/lbs
Banana          0.89
Paech           0.79
Kiwi            1.50
Pineapple       1.29
Apple           0.99

[root@oracle9i shell]# cat fruit_prices.txt
Fruit           Price/lbs
Banana          0.89
Paech           0.79
Kiwi            1.50
Pineapple       1.29
Apple           0.99

[root@oracle9i shell]# sed 's/Paech/Peach/' fruit_prices.txt
Fruit           Price/lbs
Banana          0.89
Peach           0.79
Kiwi            1.50
Pineapple       1.29
Apple           0.99

[root@oracle9i shell]# cat nash.txt
things that are eqal to the same thing are eqal to each other
[root@oracle9i shell]# sed 's/eqal/equal/' nash.txt
things that are equal to the same thing are eqal to each other
[root@oracle9i shell]# sed 's/eqal/equal/g' nash.txt
things that are equal to the same thing are equal to each other

[root@oracle9i shell]# cat fruit_prices.txt                                    
Fruit           Price/lbs
Banana          0.89
Paech           0.79
Kiwi            1.50
Pineapple       1.29
Apple           0.99
Mango           2.20
[root@oracle9i shell]# sed 's/ *[0-9][0-9]*\.[0-9][0-9]$/\$&/' fruit_prices.txt
Fruit           Price/lbs
Banana          $0.89
Paech           $0.79
Kiwi            $1.50
Pineapple       $1.29
Apple           $0.99
Mango           $2.20

[root@oracle9i shell]# cat fruit_prices.txt
Fruit           Price/lbs
Banana          0.89
Paech           0.79
Kiwi            1.50
Pineapple       1.29
Apple           0.99
Mango           2.20

[root@oracle9i shell]# sed -e 's/Paech/Peach/' -e 's/ *[0-9][0-9]*\.[0-9][0-9]$/\$&/' fruit_prices.txt
Fruit           Price/lbs
Banana          $0.89
Peach           $0.79
Kiwi            $1.50
Pineapple       $1.29
Apple           $0.99
Mango           $2.20

[root@oracle9i shell]# mv fruit_prices.txt fruit_prices.txt.0
[root@oracle9i shell]# sed -e 's/Paech/Peach/' -e 's/ *[0-9][0-9]*\.[0-9][0-9]$/\$&/' fruit_prices.txt > fruit_prices.txt                     [root@oracle9i shell]# cat fruit_prices.txt
[root@oracle9i shell]# sed -e 's/Paech/Peach/' -e 's/ *[0-9][0-9]*\.[0-9][0-9]$/\$&/' fruit_prices.txt.0 > fruit_prices.txt
[root@oracle9i shell]# cat fruit_prices.txt
Fruit           Price/lbs
Banana          $0.89
Peach           $0.79
Kiwi            $1.50
Pineapple       $1.29
Apple           $0.99
Mango           $2.20

[oracle@oracle9i ~]$ id
uid=501(oracle) gid=501(oinstall) groups=501(oinstall),502(dba)
[oracle@oracle9i ~]$ id | sed 's/(.*$//'
uid=501
[oracle@oracle9i ~]$ id | sed -e 's/(.*$//' -e 's/^uid=//'
501

 

 

 

 

[oracle@redhat4 test_crontab]$ 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
[oracle@redhat4 test_crontab]$ sed '1,3d' 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
[oracle@redhat4 test_crontab]$ sed -n '/[Mm]ain/p' datafile
northwest       NW      Charles Main            3.0     .98     3       34
northeast       NE      AM Main Jr.             5.1     .94     3       13

 

 


[oracle@redhat4 test_crontab]$ 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
[oracle@redhat4 test_crontab]$ 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.

 

[oracle@redhat4 test_crontab]$ 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.


[oracle@redhat4 test_crontab]$ sed '3,$d' datafile
northwest       NW      Charles Main            3.0     .98     3       34
western         WE      Sharon Gray             5.3     .97     5       23


[oracle@redhat4 test_crontab]$ 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.

 

[oracle@redhat4 test_crontab]$ 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


[oracle@redhat4 test_crontab]$ 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

[oracle@redhat4 test_crontab]$ cat datafile_sed_s
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
northwest west  NW      Charles Main            3.0     .98     3       34
[oracle@redhat4 test_crontab]$ sed 's/west/north/' datafile_sed_s
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
northnorth west NW      Charles Main            3.0     .98     3       34
[oracle@redhat4 test_crontab]$ sed 's/west/north/g' datafile_sed_s
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
northnorth north        NW      Charles Main            3.0     .98     3       34


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.


[oracle@redhat4 test_crontab]$ 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.


[oracle@redhat4 test_crontab]$ sed 's/[0-9][0-9]$/&.5/p' datafile
northwest       NW      Charles Main            3.0     .98     3       34.5
northwest       NW      Charles Main            3.0     .98     3       34.5
western         WE      Sharon Gray             5.3     .97     5       23.5
western         WE      Sharon Gray             5.3     .97     5       23.5
southwest       SW      Lewis Dalsass           2.7     .8      2       18.5
southwest       SW      Lewis Dalsass           2.7     .8      2       18.5
southern        SO      Suan Chin               5.1     .95     4       15.5
southern        SO      Suan Chin               5.1     .95     4       15.5
southeast       SE      Patricia Hemenway       4.0     .7      4       17.5
southeast       SE      Patricia Hemenway       4.0     .7      4       17.5
eastern         EA      TB Savage               4.4     .84     5       20.5
eastern         EA      TB Savage               4.4     .84     5       20.5
northeast       NE      AM Main Jr.             5.1     .94     3       13.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
central         CT      Ann Stephens            5.7     .94     5       13.5

EXPLANATION

The ampersand (&) 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.


[oracle@redhat4 test_crontab]$ 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.

[oracle@redhat4 test_crontab]$ 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.


[oracle@redhat4 test_crontab]$ 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.

[oracle@redhat4 test_crontab]$ sed -n '/west/,/east/p' 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
[oracle@redhat4 test_crontab]$ sed -n '/east/,/west/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
north           NO      Margot Weber            4.5     .89     5       9
central         CT      Ann Stephens            5.7     .94     5       13

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.

[oracle@redhat4 test_crontab]$ 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.

[oracle@redhat4 test_crontab]$ sed '/west/,/east/s/$/**VACA**/' datafile
northwest       NW      Charles Main            3.0     .98     3       34**VACA**
western         WE      Sharon Gray             5.3     .97     5       23**VACA**
southwest       SW      Lewis Dalsass           2.7     .8      2       18**VACA**
southern        SO      Suan Chin               5.1     .95     4       15**VACA**
southeast       SE      Patricia Hemenway       4.0     .7      4       17**VACA**
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.


[oracle@redhat4 test_crontab]$ 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.


[oracle@redhat4 test_crontab]$ cat newfile
 –––––––––––––––––––––––––––––––––––

 

                      |  ***SUAN HAS LEFT THE COMPANY***  |

                      |___________________________________|


[oracle@redhat4 test_crontab]$ 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.


[oracle@redhat4 test_crontab]$ cat newfile
 –––––––––––––––––––––––––––––––––––

 

                      |  ***SUAN HAS LEFT THE COMPANY***  |

                      |___________________________________|


[oracle@redhat4 test_crontab]$ sed -n '/north/w newfile' datafile
[oracle@redhat4 test_crontab]$ 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.

[oracle@redhat4 test_crontab]$ sed '/^north/a\
> THE NORTH SALES DISTRICT HAS MOVED<---' datafile
northwest       NW      Charles Main            3.0     .98     3       34
THE NORTH SALES DISTRICT HAS MOVED<---
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
THE NORTH SALES DISTRICT HAS MOVED<---
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. 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.If more than one line is appended, each line, except the last one, must also end with a backslash.


[oracle@redhat4 test_crontab]$ sed '/eastern/i\
> NEW ENGLANG 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 ENGLANG 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.)


[oracle@redhat4 test_crontab]$ 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.)


[oracle@redhat4 test_crontab]$ sed '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' 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


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.


[oracle@redhat4 test_crontab]$ 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.


[oracle@redhat4 test_crontab]$ 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.

[oracle@redhat4 test_crontab]$ 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.


[oracle@redhat4 test_crontab]$ 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.


[oracle@redhat4 test_crontab]$ 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.

[oracle@redhat4 test_crontab]$ 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.)


[oracle@redhat4 test_crontab]$ 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.

 


[oracle@redhat4 test_crontab]$ cat blankline
test

good
[oracle@redhat4 test_crontab]$ sed '/^ *$/d' blankline
test
good

EXPLANATION

Delete the blankline

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7865774/viewspace-613332/,如需转载,请注明出处,否则将追究法律责任。

上一篇: vmstat详细说明
下一篇: awk的一些例子
请登录后发表评论 登录
全部评论

注册时间:2008-07-11

  • 博文量
    97
  • 访问量
    182078