追求卓越,成功就会在不经意间追上你
全部博文(112)
分类: Python/Ruby
2011-05-08 21:40:12
嗯嗯,现在让我们用例子说话。
真是不做不知道呀……麻烦一大堆!!!
下面是要用到的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
##################################
READY GO!!!!
e.g. 1:
a\--找到以north开头的行并在其后添加很多FUCK
[fedora@novice chap04]$ sed '/^north/a\FUCK FUCK FUCK FUCK \nkcuf kcuf kcuf kcuf' datafile
northwest NW Charles Main 3.0 .98 3 34
FUCK FUCK FUCK FUCK
kcuf kcuf kcuf kcuf
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
FUCK FUCK FUCK FUCK
kcuf kcuf kcuf kcuf
north NO Margot Weber 4.5 .89 5 9
FUCK FUCK FUCK FUCK
kcuf kcuf kcuf kcuf
central CT Ann Stephens 5.7 .94 5 13
***************************************************
*发现在bash中只能像上面那样输入且a后的\可有可无 ;
*并不像书上说的那样,不知是不是跟SHELL有关系
*一般,a后是要带\的,有时还要带两个。如果要添加的
*文本不止一行的话,除了最后一行每一行的结尾都要跟\。
***************************************************
e.g. 2:
i\ --在以central开头的行前分行插入FUCK
[fedora@novice chap04]$ sed '/central/i\F\nU\nC\nK' 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
F
U
C
K
central CT Ann Stephens 5.7 .94 5 13
***************************************************
*一般,如果要添加的文本不止一行的话,除了最后一行
*每一行的结尾都要跟\。但在我这里好像有点不一样
***************************************************
e.g. 3:
c\--把有sourth的行全部用FUCK取代
[fedora@novice chap04]$ sed '/south/c\FUCK' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
FUCK
FUCK
FUCK
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
***********************************************
*其它与e.g.1 ,e.g. 2 类似
***********************************************
e.g. 4:
d--删除第一行
[fedora@novice chap04]$ sed '1d' 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
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
d--删除1-4行
[fedora@novice chap04]$ sed '1,4d' 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
d--删除有north的行
[fedora@novice chap04]$ 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
e.g. 5:
s--替换。用FUCK替换north.
[fedora@novice chap04]$ sed 's/north/FUCK/' datafile
FUCKwest 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
FUCKeast NE AM Main Jr. 5.1 .94 3 13
FUCK NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
s--替换。用FUCK替换所有以south开头的行中的south.
[fedora@novice chap04]$ sed '/^south/s/south/FUCK/g' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
FUCKwest SW Lewis Dalsass 2.7 .8 2 18
FUCKern SO Suan Chin 5.1 .95 4 15
FUCKeast 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
s--替换。 寻找大于1少于10的一位小数并用FUCK+&替代。这里的&保存了前面的小数
[fedora@novice chap04]$ sed 's/[0-9]\.[0-9]/FUCK&/' datafile
northwest NW Charles Main FUCK3.0 .98 3 34
western WE Sharon Gray FUCK5.3 .97 5 23
southwest SW Lewis Dalsass FUCK2.7 .8 2 18
southern SO Suan Chin FUCK5.1 .95 4 15
southeast SE Patricia Hemenway FUCK4.0 .7 4 17
eastern EA TB Savage FUCK4.4 .84 5 20
northeast NE AM Main Jr. FUCK5.1 .94 3 13
north NO Margot Weber FUCK4.5 .89 5 9
central CT Ann Stephens FUCK5.7 .94 5 13
*******************************************************
*刚开始时用的是下面的语句:
*sed 's/[0-9][0-9]$/FUCK&/g' datafile,怎么弄都不成功。
* 不知是怎么回事。
*******************************************************
今天就到这吧,都要吐了。。。。。。