LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1672|回复: 4

如何一次插入多行(sed)?

[复制链接]
发表于 2004-8-11 10:15:28 | 显示全部楼层 |阅读模式
有文件a如下:
----------------------------------
my friends
hello world
haha,you are right
----------------------------------
现在欲在"hello world"之前,一次插入3行,插入内容为:
----------------------------------
my love
my brother
my sisiter
----------------------------------
sed帮助文档说是"通过在前一行之后附加一个反斜杠来添加附加行",
我是这样做的:
sed -e '/my friends/i\my love\my brother\my sister' a
但是,却不能够一次插入3行,请大家帮我指出错误。
谢谢!
^_^
发表于 2004-8-11 11:06:15 | 显示全部楼层

  1. [shell]$ sed -e '/hello world/i\my love\
  2. > my brother\
  3. > my sister\'  file
复制代码

反斜杠之后要回车
 楼主| 发表于 2004-8-11 12:19:13 | 显示全部楼层
可是,我这个主要是要自动化,如果按照 doubleelec兄所说,则需人工
干预。
我的想法是,把脚本写好,然后就不要再人为的敲入回车了。
如果可以在一行的代码搞定,就好了。
还是用sed
谢谢!
发表于 2004-8-11 13:25:23 | 显示全部楼层
?? 虽然不是一行,但写在脚本里也不需要人为干预呀
要一行也可以

  1. sed 's/hello world/my love\nmy brother\nmy sister\n&/' txt
复制代码
发表于 2004-8-11 15:51:13 | 显示全部楼层
awk:

  1. /home/lee#awk '/hello world/{sub(/.*/,"my love\nmy borther\nmy sister\n&")}{print}' file
  2. my friends
  3. my love
  4. my borther
  5. my sister
  6. hello world
  7. haha,you are right
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表