Sed在匹配行前后加入一行

眉间皱痕 提交于 2020-02-10 09:24:50

a 追加内容 sed ‘/匹配词/a\要加入的内容’ example.file(将内容追加到匹配的目标行的下一行位置)
i 插入内容 sed ‘/匹配词/i\要加入的内容’ example.file 将内容插入到匹配的行目标的上一行位置)
示例:
#我要把文件的包含“chengyongxu.com”这个关键词的行前或行后加入一行,内容为“allow chengyongxu.cn”

1 #行前加
2 sed -i '/allow chengyongxu.com/i\allow chengyongxu.cn' the.conf.file
3 #行前后
4 sed -i '/allow chengyongxu.com/a\allow chengyongxu.cn' the.conf.file
 
---------------------------------------------------
1、删除指定行的上一行
sed -i -e :a -e '$!N;s/.*\n\(.*ServerName abc.com\)/\1/;ta' -e 'P;D' $file
2、删除指定字符串之间的内容
sed -i '/ServerName abc.com/,/\/VirtualHost/d' $filehttp://www.linuxso.com/shell/17542.html
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!