免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3363 | 回复: 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
2 [报告]
发表于 2010-07-19 15:18 |只看该作者
本帖最后由 lkk2003rty 于 2010-07-19 15:25 编辑

因为/\$90/p
但是最后两行  没有理由啊。。。不懂。。。。

论坛徽章:
0
3 [报告]
发表于 2010-07-19 15:21 |只看该作者
你要的结果是这个
sed -n '1,/\$90$/p' example

论坛徽章:
0
4 [报告]
发表于 2010-07-19 15:25 |只看该作者
回复 1# borefo


    是这样的~你要搜索第一行至 那个表达式的行
Sed直到最后一行都没找到,所以就一直打印
只要第一个条件满足就为TRUE

论坛徽章:
0
5 [报告]
发表于 2010-07-19 15:27 |只看该作者
再仔细看了看  兰州两次的example文件内容不一样吧。。。。

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
6 [报告]
发表于 2010-07-19 15:30 |只看该作者

论坛徽章:
0
7 [报告]
发表于 2010-07-19 15:30 |只看该作者
再仔细看了看  兰州两次的example文件内容不一样吧。。。。
lkk2003rty 发表于 2010-07-19 15:27



不好意思,文本确实发错了,现在更改过来了

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
8 [报告]
发表于 2010-07-19 15:31 |只看该作者
用awk吧,结果是你想要的。

论坛徽章:
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.

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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP