免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 22509 | 回复: 75

问一个有关sed匹配模式的话题 [复制链接]

论坛徽章:
0
发表于 2010-04-12 14:06 |显示全部楼层
我有个文件,其内容是
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

我使用如下命令,得到如下结果

  1. [caoke@Linux ~]$ sed -n '/It/,2'p tmp.sed
  2. It was an evening of splendid music and company.
复制代码
我使用如下命令,得到如下结果

  1. [caoke@Linux ~]$ sed -n '/an/,2'p tmp.sed
  2. The honeysuckle band played all night long for only $90.
  3. It was an evening of splendid music and company.
  4. The local nurse Miss P.Neave was in attendance.

  5. [color=Red]很奇怪,不是应该在第二行范围内搜索吗?[/color]
复制代码
这两个命令有什么区别

  1. sed -n '/an/,2'p tmp.sed
  2. sed -n '2,/an/'p tmp.sed
复制代码
不胜感激

论坛徽章:
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
发表于 2010-04-12 14:29 |显示全部楼层
起点和终点不同啊。

论坛徽章:
1
天秤座
日期:2013-10-23 13:20:42
发表于 2010-04-12 14:31 |显示全部楼层
[monitor@test ~]$ sed '2p' ./tmp.sed
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

[monitor@test ~]$ sed -n '2p' ./tmp.sed
It was an evening of splendid music and company.

[monitor@test ~]$ sed -n '/It/p' ./tmp.sed   
It was an evening of splendid music and company.

[monitor@test ~]$ sed -n '/It/,2p' ./tmp.sed
It was an evening of splendid music and company.

[monitor@test ~]$ sed -n '/It/,2'p ./tmp.sed  
It was an evening of splendid music and company.

[monitor@test ~]$ sed -n '/It/,3p' ./tmp.sed  
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.

[monitor@test ~]$ sed -n '/an/p' ./tmp.sed     
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
The local nurse Miss P.Neave was in attendance.

[monitor@test ~]$ sed -n '/an/2p' ./tmp.sed  
sed: -e expression #1, char 5: unknown command: `2'

[monitor@test ~]$ sed -n '/an/,2p' ./tmp.sed
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
The local nurse Miss P.Neave was in attendance.

应该很清楚了,那就是个label

论坛徽章:
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
发表于 2010-04-12 14:31 |显示全部楼层
'/It/,2' 从包含It的行到第二行
'/an/,2' 从包含an的行到第二行

论坛徽章:
0
发表于 2010-04-12 14:38 |显示全部楼层
回复 3# jerryjzm


    说实话,我还是不太明白,能不能解释下那个语句的含义

论坛徽章:
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
发表于 2010-04-12 14:42 |显示全部楼层
哪句还是不太明白?

论坛徽章:
0
发表于 2010-04-12 14:58 |显示全部楼层
回复 6# Shell_HAT


    [monitor@test ~]$ sed -n '/an/,2p' ./tmp.sed
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
The local nurse Miss P.Neave was in attendance.


拿这个来说吧,从an到第2行。但是为什么第3行也被选进来了?

论坛徽章:
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
发表于 2010-04-12 15:06 |显示全部楼层
本帖最后由 blackold 于 2010-04-12 15:18 编辑

回复 7# mr_caoke


不是第三行,是第四行。

因为匹配了两个范围。

论坛徽章:
0
发表于 2010-04-12 15:08 |显示全部楼层
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.

论坛徽章:
0
发表于 2010-04-12 15:13 |显示全部楼层
回复 8# blackold


    这句话的意思难道是匹配第2行,或者匹配an,的所有行?????????
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP