首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

sed删除指定行的下一行

2012-07-03 
sed删除指定行的上一行有这么一个需求,需要从若干个apache虚机配置文件中删除一段内容,类似下面这种Virtu

sed删除指定行的上一行
有这么一个需求,需要从若干个apache虚机配置文件中删除一段内容,类似下面这种

<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 *.confDir="/usr/local/apache2/conf"cd $Dirls *.conf | while read filedocon=`grep 'ServerName abc.com' $file | wc -l`if [ $con -gt 0 ];thenecho "delete domain in this file : $Dir/$file"sed -i -e :a -e '$!N;s/.*\n\(.*ServerName abc.com\)/\1/;ta' -e 'P;D' $filesed -i '/ServerName abc.com/,/\/VirtualHost/d' $fileelseecho "======== no change for file : "$filefidone


其中最重要的两条语句
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

热点排行