免费注册 查看新帖 |

Chinaunix

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

[学习共享] sed中N,sed and awk 2Nd Edition问题三 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-02-14 15:37 |只看该作者 |倒序浏览
hi,最近在看sed&awk第二版本,第6.1.1中有一个例子,如下:

cat testfile6
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
Look in the Owner and Operator Guide shipped with your system.
Two manuals are provided including the Owner and
Operator Guide and the User Guide.
The Owner and Operator Guide is shipped with your system.

目的:要将文本中出现的所有跨行或不跨行的 Owner and Operator Guide替换成Installation Guide

我写的语句:
sed "/Owner/ {N;s/Owner *\n*and *\n*Operator *\n*Guide/Installation Guide\n/g}" testfile6   

结果是只将第一、二处替换成功,第三、四处没有替换。结果如下:
Consult Section 3.1 in the Installation Guide
for a description of the tape drives
available on your system.
Look in the Installation Guide
shipped with your system.
Two manuals are provided including the Owner and
Operator Guide
and the User Guide.
The Owner and Operator Guide is shipped with your system.

请问原因是?

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
2 [报告]
发表于 2014-02-14 15:47 |只看该作者
因为你的脚本每次只处理两行

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
3 [报告]
发表于 2014-02-14 15:52 |只看该作者
本帖最后由 yestreenstars 于 2014-02-14 15:56 编辑

这个效果如何?
  1. $ sed -r ':1;N;$!b1;s/Owner\s+and\s+Operator\s+Guide/Installation Guide/g' i
  2. Consult Section 3.1 in the Installation Guide for a description of the tape drives
  3. available on your system.
  4. Look in the Installation Guide shipped with your system.
  5. Two manuals are provided including the Installation Guide and the User Guide.
  6. The Installation Guide is shipped with your system.
复制代码
  1. $ perl -pe 'BEGIN{$/=undef};s/Owner\s+and\s+Operator\s+Guide/Installation Guide/g' i
  2. Consult Section 3.1 in the Installation Guide for a description of the tape drives
  3. available on your system.
  4. Look in the Installation Guide shipped with your system.
  5. Two manuals are provided including the Installation Guide and the User Guide.
  6. The Installation Guide is shipped with your system.
复制代码

论坛徽章:
0
4 [报告]
发表于 2014-02-14 15:58 |只看该作者
回复 1# davidbreathe
第三个:
因为使用N读入下一个inputline后,下一次再读就读不到这一行了,就是每一行只会读一次,这么说楼主明白了吧
第四个:
已经到最后一行了,后面已经无记录,N执行失败了,后面的代码不执行

   

论坛徽章:
0
5 [报告]
发表于 2014-02-14 16:38 |只看该作者
回复 4# runintostar

呀,明白了,多谢多谢。那这么说书上有错哎


   

论坛徽章:
29
程序设计版块每日发帖之星
日期:2016-02-29 06:20:0015-16赛季CBA联赛之天津
日期:2016-08-10 10:33:1115-16赛季CBA联赛之深圳
日期:2016-08-17 15:07:2015-16赛季CBA联赛之佛山
日期:2016-11-07 11:33:5015-16赛季CBA联赛之广夏
日期:2016-11-15 09:13:31CU十四周年纪念徽章
日期:2016-11-24 14:12:25极客徽章
日期:2016-12-07 14:03:4015-16赛季CBA联赛之深圳
日期:2016-12-07 17:15:2715-16赛季CBA联赛之北京
日期:2016-12-22 09:30:0115-16赛季CBA联赛之深圳
日期:2016-12-22 10:49:2115-16赛季CBA联赛之山西
日期:2017-02-10 09:05:3215-16赛季CBA联赛之同曦
日期:2017-02-27 14:19:08
6 [报告]
发表于 2014-02-14 23:18 |只看该作者
  1. sed '/Owner/{$!N;s/Owner *\n*and *\n*Operator\( *\n*Guide\)/Installation\1/g;P;D}'
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP