Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2040316
  • 博文数量: 470
  • 博客积分: 10206
  • 博客等级: 上将
  • 技术积分: 5620
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-03 12:50
文章分类

全部博文(470)

文章存档

2012年(1)

2011年(18)

2010年(47)

2009年(404)

分类:

2010-07-25 22:52:55

cat biaoqian
AA
BC
AA
CB
CC
AA

sed '{/AA/b lable;s/$/ NO/;:lable;s/$/ YES/}' biaoqian 
匹配到AA就跳往标签,把$换成 YES,不匹配的行,就把$换成 NO

可怎么结果都会有 YES呢?
AA YES
BC NO YES
AA YES
CB NO YES
CC NO YES
AA YES

Tim:  sed '/^AA/s/$/ YES/;t;s/$/ NO/'
Tim:  sed '/^AA/ba;s/$/ NO/;b;:a;s/$/ YES/'

Hamming: 

回复 #18 blackold 的帖子

黑哥又拿小弟开涮?

lz标签就是个记号(黑哥说的"狗尿"),要注意的是


QUOTE:
b label
Branch to label; if label is omitted, branch to end of script.

t label
If a s/// has done a  successful  substitution  since  the  last input  line  was  read  and  since the last t or T command, then branch to label; if label is omitted, branch to end of script.

T label
If no s/// has done a successful  substitution  since  the  last input  line  was  read  and  since the last t or T command, then branch to label; if label is omitted, branch to end of script.


b、t和T的共同点是“if label is omitted, branch to end of script”
不同点是b无条件跳转,t和T有条件跳转

Squall:  


QUOTE:
原帖由 小木虫子 于 2009-8-7 23:29 发表 
我几乎搜了置顶的《SHELL基础十二篇》,《十三问》,《UNIX SHELL 范例4》、《UNIX SHELL第三版》、《awk 与sed》

都没有介绍,不知道是这知识点不重要,还是太难没人会。


看别人的例子看不懂,哪位朋友 ...




[root@lvs-ser1 ~]# more test
This is a label A
This is a label B
This is a label C
This is a label D


[root@lvs-ser1 ~]# sed '{   
/label/b there;      \\当匹配label时,就跳转到比标签there那,然后执行下面的s/$/ \!/语句,而s/label/LABEL/;语句就不执行了。 
s/label/LABEL/;         
:there;                  \\ 定义一个标签there。
s/$/ \!/}' test


This is a label A !
This is a label B !
This is a label C !
This is a label D !


[root@lvs-ser1 ~]# sed '{
/MM/b there;       \\当没有匹配到MM时,就不跳转到there那了,执行下一条s/label/LABEL/语句,以及:there后面的s/$/ \!/语句。
s/label/LABEL/;
:there;                \\ 定义一个标签there。
s/$/ \!/}' test


This is a LABEL A !
This is a LABEL B !
This is a LABEL C !
This is a LABEL D !

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