Chinaunix首页 | 论坛 | 博客
  • 博客访问: 708043
  • 博文数量: 112
  • 博客积分: 3889
  • 博客等级: 少校
  • 技术积分: 1448
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-19 16:35
个人简介

追求卓越,成功就会在不经意间追上你

文章分类

全部博文(112)

文章存档

2015年(1)

2014年(2)

2013年(1)

2012年(16)

2011年(86)

2010年(6)

分类: Python/Ruby

2011-05-09 20:24:11

从昨天吐的地方开始:嘿嘿

e.g. 6

p--打印:默认打印所有的行。这里用-n选项过滤无关的行

[fedora@novice chap04]$ 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

P--多行打印:在执行完所有命令后模式空间的内容会自动输出,在下面的例子中可以看到匹配的行输出了两次,但是-n选项会抑制这个动作。

[fedora@novice chap04]$ 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

***********************************************************************************


e.g. 7

D--多行删除:选项看到dD的不同之处了么?其中空行分别为1234行。

[fedora@novice chap04]$ cat >test

this is a test line

this is a test line

this is a test line

this is a test line

this ia a test line

[fedora@novice chap04]$ sed -e '/^$/N' -e '/^\n$/d' test

this is a test line

this is a test line

this is a test line

this is a test line

this ia a test line

[fedora@novice chap04]$ sed -e '/^$/N' -e '/^\n$/D' test

this is a test line

this is a test line

this is a test line

this is a test line

this ia a test line

***********************************************************************************

可以看到,与N配合的情况下:

使用d,若有偶数个空行将会全被删除,若有奇数个空行将会保留一行。这是因为d删除的是整个模式空间的内容。一旦遇到第一个空行就马上读入下一行,然后两行都删除。如果第三行为空且下一行不为空则命令不执行,空行被输出。

使用D,当遇到两个空行时D会删除两个空行中的第一个,然后再读入下一行,如果是空行则删除第一行,如果空行后有文本则模式空间可以正常输出。

***********************************************************************************

e.g. 8:关于N命令与-e选项在上面已在应用,这里就不举例啦,偷点懒哈,嘿嘿。

***********************************************************************************


e.g. 9

r--从文件中读取:从test中读取相关的内容添加到datafile中所有匹配的行的后面。

[fedora@novice chap04]$ sed '/^south/r test' 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

**********************************

FUCK FUCK FUCK FUCK ,FUCK TOO MUCH

**********************************

southern SO Suan Chin 5.1 .95 4 15

**********************************

FUCK FUCK FUCK FUCK ,FUCK TOO MUCH

**********************************

southeast SE Patricia Hemenway 4.0 .7 4 17

**********************************

FUCK FUCK FUCK FUCK ,FUCK TOO MUCH

**********************************

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. 10

w--写入文件:把datafile中所有匹配的行写入到test文件中

[fedora@novice chap04]$ sed '/^south/w test' datafile|cat test

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

***********************************************************************************


e.g. 11

n--next:如果有能匹配western行,则n命令使得sed读取下一行,然后执行相应命令

[fedora@novice chap04]$ sed -e '/western/n' -e 's/SW/FUCK/' datafile|head -n 4

northwest NW Charles Main 3.0 .98 3 34

western WE Sharon Gray 5.3 .97 5 23

southwest FUCK Lewis Dalsass 2.7 .8 2 18

southern SO Suan Chin 5.1 .95 4 15

***********************************************************************************


e.g. 12

y--变换:

[fedora@novice chap04]$ sed '3,5y/s/S/' 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


[fedora@novice chap04]$ sed '1y/3/9/' datafile

northwest NW Charles Main 9.0 .98 9 94

western WE Sharon Gray 5.3 .97 5 23

**********************************************************************************

替换的类型要一致,数字与字母之间不能相互替换。

且对正则表达式的元字符不起作用。

***********************************************************************************


e.g. 13:

q--退出:

打印三行后退出

[fedora@novice chap04]$ sed '3q' 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

用相应的字符做出替换后退出

[fedora@novice chap04]$ sed -e 'y/northwest/ABCDEFGHI/' -e q datafile

ABCDEFGHD NW CEaClGH MaiA 3.0 .98 3 34

这样也可以的

[fedora@novice chap04]$ sed '{ y/northwest/ABCDEFGHI/; q;}' datafile

ABCDEFGHD NW CEaClGH MaiA 3.0 .98 3 34

多个命令写在一行时可以用-e选项,也可以用花括号把所有命令括起来并用分号隔开且最后一个分号可有可无,在我的实验环境下。

***********************************************************************************


e.g. 14:

H/h/G/g--保存和取得

下面是这几个命令的一些组合,能看出些什么来么?

[fedora@novice chap04]$ 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

[fedora@novice chap04]$ 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

[fedora@novice chap04]$ 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

[fedora@novice chap04]$ 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


因为呢,H/G在相应空间的内容之后放置一个换行符,且后面紧跟模式空间的内容;而g/h的呢都是取代相应空间的内容,所以就有上面的不同结果喔

***********************************************************************************


e.g. 15

x--交换模式/保持空间内容

首先匹配第一个包含north的行放入保持缓存,然后匹配第一个包含south的行放入模式空间,最后把两者的内容交换。

[fedora@novice chap04]$ sed -e '/north/h' -e '/south/x' datafile

northwest NW Charles Main 3.0 .98 3 34

western WE Sharon Gray 5.3 .97 5 23

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

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. 16

sed脚本:

[fedora@novice chap04]$ vim sedlist

/central/a\

------This is a test--------

/northeast/i\

------This is a test too------

[fedora@novice chap04]$ sed -f sedlist 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

------This is a test too------

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

------This is a test--------

可以在里面添加注释,以#开头的行被认为是注释

如有多行,则每行都要以\结尾,除了最后一行。如,下面的脚本是可以正常执行的。

[fedora@novice chap04]$ cat sedlist

#This script is a test for sed commands list

/central/a\

------This is a test too--------\

------Hi am here----------------\

#now ,the second test

/northeast/i\

------This is a test ------

***********************************************************************************


阅读(1294) | 评论(2) | 转发(1) |
给主人留下些什么吧!~~

xiaozhenggang2011-05-09 22:18:38

jamesbert: .....

jamesbert2011-05-09 21:36:55