Chinaunix

标题: sed如何做这个题 [打印本页]

作者: __lxmxn__    时间: 2011-01-18 23:06
标题: sed如何做这个题
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

作者: yinyuemi    时间: 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
作者: __lxmxn__    时间: 2011-01-19 00:24
呵呵,不够灵活

如果path是变化的,或者有多个path就不成了
作者: yinyuemi    时间: 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
作者: zzy7186    时间: 2011-01-19 00:34
tac file2 | sed '/^\[file\]$/r file1' | tac
作者: yinyuemi    时间: 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
作者: yjh777    时间: 2011-01-19 00:53
回复 1# __lxmxn__


sed '/\[file/ {:l /.*\(\n\n\|$\)/! {N; b l;}; s//###\n### xxx\n###\n&/; }'  file1
作者: yjh777    时间: 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
作者: __lxmxn__    时间: 2011-01-19 10:40
嗯,多谢 yjh777 兄~

感觉可以利用sed的hold space和pattern space来做到,但最终尝试失败。
作者: BangBull    时间: 2011-01-20 18:06
  1. sed '/\[file\]/s//###\n### here is a section\n###\n&/' urfile
复制代码

作者: cjaizss    时间: 2011-01-20 20:13
嗯,多谢 yjh777 兄~

感觉可以利用sed的hold space和pattern space来做到,但最终尝试失败。
__lxmxn__ 发表于 2011-01-19 10:40



    r命令不能把文件内容导入到这两个空间
作者: 禁飞区    时间: 2011-01-21 15:46
sed '/^\[/i\###\n###\ here is a section\n###'  yourfile

我注意到 ###
              [file]  这两行是是紧挨着的.
作者: __lxmxn__    时间: 2011-01-24 22:04
回复 11# cjaizss

但是可以把匹配到的文本访问hot space,r读了文件之后,再把文本打印出来,就是不知道如何实现。。
作者: cjaizss    时间: 2011-01-24 23:19
回复  cjaizss

但是可以把匹配到的文本访问hot space,r读了文件之后,再把文本打印出来,就是不知道如 ...
__lxmxn__ 发表于 2011-01-24 22:04



    hot space?什么东西?
作者: cjaizss    时间: 2011-01-24 23:20
hot space?什么东西?
cjaizss 发表于 2011-01-24 23:19



    你的意思是hold space吧,但你这样打印会多一行空至少是空行的东西
作者: __lxmxn__    时间: 2011-01-25 14:25
回复 14# cjaizss

哈哈,说错了,是hold space
作者: blackold    时间: 2011-01-25 15:09
本帖最后由 blackold 于 2011-01-25 15:16 编辑

非要用hold space?
  1. sed '/\[file\]/,+1{//{r file2
  2. ;h;d;};x;G}' file1
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2