免费注册 查看新帖 |

Chinaunix

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

发道题目,大家共勉一下! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-20 20:40 |只看该作者 |倒序浏览
问题:
test.txt  源文件如下:
#more test.txt
I am a student , I like UNIX , I aways read the User and Operation
Documents , I know it is very important for me .
But , I do not like the User and Operation Documents . Since it is so
difficult for reading and understanding .
Last Sunday , one of my friend told me that the User and
Operation
Documents
will upgraded to the Version 3.1 .

文件中的换行符号不可以改变。请规整的替换User and Operation Documents
为Instruction Documents 将文件改变为下列的格式(注意格式不可以乱更改):

希望改变为:

#more test.txt
I am a student , I like UNIX , I aways read the Instruction Documents ,
I know it is very important for me .
But , I do not like the Instruction Documents. Since it is so
difficult for reading and understanding .
Last Sunday , one of my friend told me that the Instruction Documents
will upgraded to the Version 3.1 .

就是你不可以修改源文件的换行符哦!
呵呵,保持源文件格式哦!

想想看吧!

PS: 其实,这道题目就是考验,当你的替换文件中的替换目标,不在一行时候的sed解决办法。


题目的答案在第五页哦 !题目的答案在第五页哦 !题目的答案在第五页哦 !题目的答案在第五页哦 !题目的答案在第五页哦 !

[ 本帖最后由 stephen412 于 2006-8-22 20:39 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-08-20 21:13 |只看该作者
你的意思是只替换中间的User and Operation Documents 吗
还是全部要替换,如果全部替换
User and
Operation
Documents
要成什么样子才不违反规则呢

论坛徽章:
0
3 [报告]
发表于 2006-08-20 23:38 |只看该作者
我想是变成:
  1. Last Sunday , one of my friend told me that the
  2. Instruction
  3. Documents
复制代码


笨点的办法
  1. sed -e 's/User and/Instruction/g1' -e 's/User and/Instruction/g2'  -e 's/User and//g3' -e 's/Operation/Instruction/g3' u_file
复制代码


不对,不能用简单的使用sed

[ 本帖最后由 scode 于 2006-8-21 08:06 编辑 ]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:56:11
4 [报告]
发表于 2006-08-21 05:49 |只看该作者
脚本:
FNM=bb
DC=bb1

awk -F# -v DIC=$DC 'BEGIN {
        cnt = 0
        while(getline < DIC > 0)
        {       a[cnt] = $1
                b[cnt++] = $2
        }
        total= cnt
}
{       for(i = 0; i < total; i++)  gsub(a[i], b[i], $0)
        printf("%s\n", $0)
        while(getline > 0)
        {       for(i = 0; i < total; i++)  gsub(a[i], b[i], $0)
                printf("%s\n", $0)
        }
}' $FNM

文件bb1可以看作词典,这里是如下的一行:
User and Operation Documents#Instruction Documents

这是造词典的万能替换法。

论坛徽章:
0
5 [报告]
发表于 2006-08-21 08:38 |只看该作者
OK !我再说清楚一些!

问题:
test.txt  文件如下:
#more test.txt
I am a student , I like UNIX , I aways read the User and Operation
Documents , I know it is very important for me .
But , I do not like the User and Operation Documents . Since it is so
difficult for reading and understanding .
Last Sunday , one of my friend told me that the User and
Operation
Documents
will upgraded to the Version 3.1 .

改变为:

#more test.txt
I am a student , I like UNIX , I aways read the Instruction Documents ,
I know it is very important for me .
But , I do not like the Instruction Documents. Since it is so
difficult for reading and understanding .
Last Sunday , one of my friend told me that the Instruction Documents
will upgraded to the Version 3.1 .

:-) Is anybody clear ?
提示:楼上的用的太复杂了,使用模式空间和保持空间,或者使用next 模式哦!
会简单很多哦!

[ 本帖最后由 stephen412 于 2006-8-21 15:14 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2006-08-21 08:39 |只看该作者
原帖由 scode 于 2006-8-20 23:38 发表
我想是变成:
  1. Last Sunday , one of my friend told me that the
  2. Instruction
  3. Documents
复制代码


笨点的办法
[code]sed -e 's/User and/Instruction/g1' -e 's/User and/Instruction/g2'  -e 's/Us ...



聪明哦!
呵呵!
但是不可以用简单的sed哦!那样子是没有办法成功的哦!

[ 本帖最后由 stephen412 于 2006-8-21 08:41 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2006-08-21 09:13 |只看该作者
顶2楼的,楼主没有说明白

论坛徽章:
0
8 [报告]
发表于 2006-08-21 09:50 |只看该作者
那么我现在总是说明白了吧!

大家赶紧抓紧时间思考吧!这个题目其实他的内涵很丰富阿!
他让我们可以更加深层次的认识sed,了解模式空间和保持空间,以及next模式的概念哦!
我觉得对我们大家是有好处的!

[ 本帖最后由 stephen412 于 2006-8-21 09:53 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2006-08-21 12:18 |只看该作者
没有人回答阿!
看来,这个帖子要沉掉了。
这样子吧,周末如果没有人回答出答案,我就公布一下,然后将整个分析的思路也写出来。
共勉一下!

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
10 [报告]
发表于 2006-08-21 12:50 |只看该作者
原帖由 stephen412 于 2006-8-21 08:38 发表
OK !我再说清楚一些!

问题:
test.txt  文件如下:
#more test.txt
I am a student , I like UNIX , I aways read the User and Operation
Documents, I know it is very important for me .
But , I do not like the User and Operation Documents . Since it is so
difficult for reading and understanding .
Last Sunday , one of my friend told me that the User and
Operation
Documents
will upgraded to the Version 3.1 .

改变为:

#more test.txt
I am a student , I like UNIX , I aways read the Instruction Documents
I know it is very important for me .
But , I do not like the Instruction Documents. Since it is so
difficult for reading and understanding .
Last Sunday , one of my friend told me that the Instruction Documents
will upgraded to the Version 3.1 .

:-) Is anybody clear ?
提示:楼上的用的太复杂了,使用模式空间和保持空间,或者使用next 模式哦!
会简单很多哦!

你确定你真的说明白了吗?
如上,红色的逗号和其后跟随的空格是忘记敲了还是故意要丢掉?^_^第一个Documents后要换行这一点似乎没有什么疑问。
如果没有错误,这样的一行perl应该就搞定了:
perl -0pe 's/User\sand\sOperation\sDocuments(.\s*)/Instruction Documents\n/gs'
多行的操作用perl最方便。用sed虽然要麻烦点,但也远不是什么难题,我就不写了。^_^
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP