有这么一个需求,需要从若干个apache虚机配置文件中删除一段内容,类似下面这种
思路:以ServerName为中心,删除上面的一行,再删除从ServerName到</VirtualHost>之间的内容即可。
脚本如下:
其中最重要的两条语句
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' $file
<VirtualHost *>
ServerName abc.com
DocumentRoot /home/apache/abc
CustomLog logs/abc.com-access_log combined
</VirtualHost>
思路:以ServerName为中心,删除上面的一行,再删除从ServerName到</VirtualHost>之间的内容即可。
脚本如下:
#! /bin/sh
# by logo32.iteye.com
# delete domain from *.conf
Dir="/usr/local/apache2/conf"
cd $Dir
ls *.conf | while read file
do
con=`grep 'ServerName abc.com' $file | wc -l`
if [ $con -gt 0 ];then
echo "delete domain in this file : $Dir/$file"
sed -i -e :a -e '$!N;s/.*\n\(.*ServerName abc.com\)/\1/;ta' -e 'P;D' $file
sed -i '/ServerName abc.com/,/\/VirtualHost/d' $file
else
echo "======== no change for file : "$file
fi
done
其中最重要的两条语句
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' $file