Chinaunix

标题: sed的模板确定范围内的被打印无法理解 [打印本页]

作者: 木木老蔫    时间: 2012-12-05 17:26
标题: sed的模板确定范围内的被打印无法理解
sed -n '/test/,/check/p' example-----所有在模板test和check所确定的范围内的行都被打印。
上面是例子
如果我example里面是
a 2 b 3 c 4
ab 8 b 6 c 7
c 5 d 2 b 1
我的命令是
sed -n '/a/,/b/p' a.txt
会发现第三行没被打印出来
求解
作者: cuteorange    时间: 2012-12-05 17:29
回复 1# 木木老蔫


    因为第三行要进行第二轮(/a/,/b/)匹配,没找到a,所以打印不出来
作者: Shell_HAT    时间: 2012-12-05 17:30
地址范围的right operand是pattern的时候:
http://bbs.chinaunix.net/viewthread.php?tid=1689518#pid12055900

range operator maintains its own Boolean state.
It is false as long as its left operand is false. Once the left operand is true, the range operator stays true until the right operand is true, after which the range operator becomes false again.
The operator doesn't become false until the next time it is evaluated.
作者: 木木老蔫    时间: 2012-12-05 17:55
是不是第三轮啊,每行都要匹配?回复 2# cuteorange


   
作者: Shell_HAT    时间: 2012-12-05 18:44
回复 4# 木木老蔫


    对于/a/来说是第二轮
作者: 木木老蔫    时间: 2012-12-05 19:00
我还是有点不理解
第一行有个a,第二行也有个a,为什么到第三行的匹配还是第二轮
回复 5# Shell_HAT


   
作者: Shell_HAT    时间: 2012-12-05 19:02
回复 6# 木木老蔫


第一行:/a/中
从第二行开始,判断/b/,中
从第三行开始,判断/a/,不中
作者: 木木老蔫    时间: 2012-12-05 19:06
我明白咧,谢谢大神相告回复 7# Shell_HAT


   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2