免费注册 查看新帖 |

Chinaunix

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

sed难题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-10 01:21 |只看该作者 |倒序浏览
文件file,内容如下:
开头省略N行……
parameters dstport=80
servername pp.com
listenport 8080
docroot /var/www/htdocs/pp
.....


现在想用sed实现:

找到8080的行,添加内容相同的一行到此行下面,但8080被替换成80,howto?

变成:
parameters dstport=80
servername pp.com
listenport 8080
listenport 80
docroot /var/www/htdocs/pp

论坛徽章:
0
2 [报告]
发表于 2010-06-10 01:42 |只看该作者
回复 1# __lxmxn__


   
5.5 Append, Insert, and Change
The append (a), insert (i), and change (c) commands provide editing functions that are commonly performed with an interactive editor, such as vi. You may find it strange to use these same commands to "enter" text using a noninteractive editor. The syntax of these commands is unusual for sed because they must be specified over multiple lines. The syntax follows:

append [line-address]a\
text
insert [line-address]i\
text
change [address]c\
text


你的这个就是:

/listenport 8080/a\
listenport 80

论坛徽章:
0
3 [报告]
发表于 2010-06-10 07:49 |只看该作者

  1. 1!{x;/8080/s/8080/80/gp;x};h;
复制代码

求职 : 技术支持/维
论坛徽章:
0
4 [报告]
发表于 2010-06-10 08:13 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2010-06-10 08:50 |只看该作者
本帖最后由 bbgg1983 于 2010-06-10 08:54 编辑
  1. $ sed '/8080/a\listenport 80' file
  2. parameters dstport=80
  3. servername pp.com
  4. listenport 8080
  5. listenport 80
  6. docroot /var/www/htdocs/pp
复制代码

论坛徽章:
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
6 [报告]
发表于 2010-06-10 08:54 |只看该作者

论坛徽章:
0
7 [报告]
发表于 2010-06-10 08:56 |只看该作者
[root@commserver: /tmp]#sed 's/8080$/&\nlistenport 80/' test117.txt
dfsdfsad
dfdf
parameters dstport=80
servername pp.com
listenport 8080
listenport 80
docroot /var/www/htdocs/pp

论坛徽章:
0
8 [报告]
发表于 2010-06-10 09:17 |只看该作者
一楼问的是下面的第四行为什么会是listenport 80
parameters dstport=80
servername pp.com
listenport 8080
listenport 80
docroot /var/www/htdocs/pp

而真正想要的结果是:
parameters dstport=80
servername pp.com
listenport 8080
listenport 8080
docroot /var/www/htdocs/pp

论坛徽章:
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
9 [报告]
发表于 2010-06-10 09:51 |只看该作者
最好别把listenport写死
3楼的漂亮

论坛徽章:
0
10 [报告]
发表于 2010-06-10 09:57 |只看该作者
本帖最后由 laohuanggua 于 2010-06-10 10:39 编辑
sed我还没入门
用awk写了一个
awk '/8080/{a=$0;sub("8080","80",a);print $0"\n"a;next}1'
99超人 发表于 2010-06-10 08:13


我的和你类似
cat datafile2
parameters dstport=80
servername pp.com
listenport1 8080
docroot /var/www/htdocs/pp
listenport2 8080
listenport 8081
awk '1;/8080/{gsub(8080,80);print}' datafile2
parameters dstport=80
servername pp.com
listenport1 8080
listenport1 80
docroot /var/www/htdocs/pp
listenport2 8080
listenport2 80
listenport 8081
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP