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 ! |
|
|