免费注册 查看新帖 |

Chinaunix

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

sed如何做这个题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-18 23:06 |只看该作者 |倒序浏览
file1内容:
[file]
path = /var/lib/mysql
comment = mysql server

[file]
path = /var/named
comment = bind data directory



file2内容:
###
### here is a section
###


sed如何将file1编辑成如下?
###
### here is a section
###
[file]
path = /var/lib/mysql
comment = mysql server

###
### here is a section
###
[file]
path = /var/named
comment = bind data directory

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
2 [报告]
发表于 2011-01-19 00:17 |只看该作者
$ sed '/file/ {r file2
d}
/path/i\
[file] ' file1
###
### here is a section
###
[file]
path = /var/lib/mysql
comment = mysql server

###
### here is a section
###
[file]
path = /var/named
comment = bind data directory

论坛徽章:
0
3 [报告]
发表于 2011-01-19 00:24 |只看该作者
呵呵,不够灵活

如果path是变化的,或者有多个path就不成了

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
4 [报告]
发表于 2011-01-19 00:34 |只看该作者
$ awk 'NR==FNR{a["[file]"]=$0"\n"a["[file]"] }NR>FNR{print a[$0]?a[$0]"\n"$00}' file2 file1
###
### here is a section
###

[file]
path = /var/lib/mysql
comment = mysql server

###
### here is a section
###

[file]
path = /var/named
comment = bind data directory

论坛徽章:
0
5 [报告]
发表于 2011-01-19 00:34 |只看该作者
tac file2 | sed '/^\[file\]$/r file1' | tac

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
6 [报告]
发表于 2011-01-19 00:41 |只看该作者
回复 3# __lxmxn__

try this:

$ sed '/file/ {r file2
a [file]
d}' file1
###
### here is a section
###
[file]
path = /var/lib/mysql
comment = mysql server

###
### here is a section
###
[file]
path = /var/named
comment = bind data directory

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
7 [报告]
发表于 2011-01-19 00:53 |只看该作者
回复 1# __lxmxn__


sed '/\[file/ {:l /.*\(\n\n\|$\)/! {N; b l;}; s//###\n### xxx\n###\n&/; }'  file1

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
8 [报告]
发表于 2011-01-19 01:58 |只看该作者
本帖最后由 yjh777 于 2011-01-19 02:36 编辑

sed "s/\[file/`   sed  ':l; N; $! b l; s/\\n/\\\\n/g'  file2  `\n&/" file1



-------------------------------------------------------------------------------------------------------
注意 ``  $() 区别, 直接$()就可以, 用``时还得将\再转义}
sed 's/\[file/'"$(   sed  ':l; N; $! b l; s/\n/\\n/g'  file2   )"'\n&/' file1

headStr=$(sed  ':l; N; $! b l; s/\n/\\n/g'  file2)
sed "s/\[file/${headStr}\\n&/" file1

论坛徽章:
0
9 [报告]
发表于 2011-01-19 10:40 |只看该作者
嗯,多谢 yjh777 兄~

感觉可以利用sed的hold space和pattern space来做到,但最终尝试失败。

论坛徽章:
0
10 [报告]
发表于 2011-01-20 18:06 |只看该作者
  1. sed '/\[file\]/s//###\n### here is a section\n###\n&/' urfile
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP