Sed在匹配行前后加入一行

来源:互联网 发布:淘宝店铺模版怎么修改 编辑:程序博客网 时间:2024/05/24 05:26
a 追加内容 sed ‘/匹配词/a\要加入的内容’ example.file(将内容追加到匹配的目标行的下一行位置)

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

1#行前加
2sed -i '/allow chengyongxu.com/i\allow chengyongxu.cn' the.conf.file
3#行前后
4sed -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

0 0