Sed在匹配行前后加入一行

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
posted @ 2020-02-10 09:06  30岁再次出发  阅读(1954)  评论(0编辑  收藏  举报