sed 批量修改文件

#!/bin/bash

Judge()
{
if [ $? -ne 0 ];then
echo ${i} >>error.log
fi

}

sed -i "/CLOUDWISE_REQUEST_INFO/s/^/#/" nginx.default.conf   ##注释含有CLOUDWISE_REQUEST_INFO 的行
sed -i 's/warn//' ssl_host/nginx.default.conf    ##删除某一行中字符串warn

##修改匹配到的某字符串所在行的下一行
num=`grep -n "static" nginx.default.conf | awk -F ':' '{print $1}'`
sed -i "$((${num}+1))c\ proxy_pass http://localhost;"  nginx.default.conf

##根据匹配到的某一行,在其上N行插入需要的location模块
num=$(cat vhost/nginx.default.conf |grep -n -w "static" | awk -F : '{print $1}')
sed -i "${num}i\ location ~* ^/system-manager/ {" nginx.default.conf
sed -i "$((${num}+1))i\ proxy_pass http://localhost;" nginx.default.conf
sed -i "$((${num}+2))i\ proxy_set_header Host \"  www.myself.com\";" nginx.default.conf
sed -i "$((${num}+3))i\ }" nginx.default.conf
sed -i "$((${num}+4))i\ " nginx.default.conf

Judge

done

 

引申:

在匹配行后添加,不需要像上面一样确定行号

seq 5 |sed '3s/.*/&\ntxt/'

posted @ 2019-08-16 16:36  rockstics  阅读(390)  评论(0编辑  收藏  举报