免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3367 | 回复: 16
打印 上一主题 下一主题

sed匹配问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-19 15:15 |只看该作者 |正序浏览
本帖最后由 borefo 于 2010-07-19 15:29 编辑

  1. linux-svy2:/opt/sed # cat example
  2. The honeysuckle band played all night long for only $90.
  3. It was an evening of splendid music and company.
  4. Too of bad the disco floor fell through at 23:00.
  5. The local nurse Miss P.Neave was in attendance.
复制代码
执行下面为什么会有下面结果?

  1. linux-svy2:/opt/sed # sed -n '1, /\$90/p' example
  2. The honeysuckle band played all night long for only $90.
  3. It was an evening of splendid music and company.
  4. Too of bad the disco floor fell through at 23:00.
  5. The local nurse Miss P.Neave was in attendance.
复制代码
按照我的理解,应该只打印第一行,为什么全部打印出来了呢?

论坛徽章:
0
17 [报告]
发表于 2010-10-05 23:28 |只看该作者
学习了 呵呵

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
16 [报告]
发表于 2010-07-21 11:42 |只看该作者
地址范围的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.

实例:
http://bbs.chinaunix.net/viewthr ... ;page=7#pid12057544

论坛徽章:
0
15 [报告]
发表于 2010-07-21 10:45 |只看该作者
学习了 ,平时不注意还真不清楚

论坛徽章:
2
IT运维版块每日发帖之星
日期:2016-02-10 06:20:01IT运维版块每日发帖之星
日期:2016-02-11 06:20:00
14 [报告]
发表于 2010-07-21 10:28 |只看该作者
学习了

论坛徽章:
0
13 [报告]
发表于 2010-07-21 10:12 |只看该作者
回复 9# sosodream


    佩服老兄

论坛徽章:
20
CU大牛徽章
日期:2013-04-17 11:48:26羊年新春福章
日期:2015-03-10 22:39:202015年中国系统架构师大会
日期:2015-06-29 16:11:282015亚冠之平阳省
日期:2015-07-31 09:19:042015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-09-30 06:20:002015亚冠之柏太阳神
日期:2015-10-19 20:29:5915-16赛季CBA联赛之天津
日期:2016-11-29 14:03:4315-16赛季CBA联赛之北控
日期:2016-12-24 20:51:492015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-12 20:58:532014年中国系统架构师大会
日期:2014-10-14 15:59:00
12 [报告]
发表于 2010-07-20 20:59 |只看该作者
试了一下,因为你的第1行和$90是同一行,第1行是肯定要打出来的,而$90是从第2行开始查找,结果直到最后也没 ...
jy02107028 发表于 2010-07-19 16:19



   哥们解释得很详细,明白了

论坛徽章:
0
11 [报告]
发表于 2010-07-19 16:32 |只看该作者
要第一行为啥不行sed -n '1p' file

论坛徽章:
0
10 [报告]
发表于 2010-07-19 16:19 |只看该作者
试了一下,因为你的第1行和$90是同一行,第1行是肯定要打出来的,而$90是从第2行开始查找,结果直到最后也没找到,因此全部打出来了

论坛徽章:
0
9 [报告]
发表于 2010-07-19 16:19 |只看该作者
本帖最后由 sosodream 于 2010-07-19 16:20 编辑

If the second address is a REGEXP, then checking for the ending
match will start with the line _following_ the line which matched the
first address: a range will always span at least two lines (except of
course if the input stream ends).

(节选info sed)

当第二段地址为正则时,第二段地址的匹配从第一个地址生效“下一行”为起始

用楼主的例子还说,就是,1匹配了第一行,但/\$90/是从第二行开始匹配的

解决的方法:
1改成0


说明:
`0,/REGEXP/'
     A line number of `0' can be used in an address specification like
     `0,/REGEXP/' so that `sed' will try to match REGEXP in the first
     input line too.  In other words, `0,/REGEXP/' is similar to
     `1,/REGEXP/', except that if ADDR2 matches the very first line of
     input the `0,/REGEXP/' form will consider it to end the range,
     whereas the `1,/REGEXP/' form will match the beginning of its
     range and hence make the range span up to the _second_ occurrence
     of the regular expression.
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP