Regex SED:从httpd-vhosts.conf中删除虚拟主机

Regex SED:从httpd-vhosts.conf中删除虚拟主机,regex,bash,sed,Regex,Bash,Sed,我正在尝试使用sed从httpd-vhosts.conf中删除一个虚拟主机项,并使用正确的匹配正则表达式 我想删除这样的块: <VirtualHost *:80> ServerName local.testsite.org DocumentRoot "/home/sites/local.testsite.org/www" ErrorLog "/home/sites/local.testsite.org/logs/error_log" CustomLog

我正在尝试使用sed从httpd-vhosts.conf中删除一个虚拟主机项,并使用正确的匹配正则表达式

我想删除这样的块:

<VirtualHost *:80>
    ServerName local.testsite.org
    DocumentRoot "/home/sites/local.testsite.org/www"
    ErrorLog "/home/sites/local.testsite.org/logs/error_log"
    CustomLog "/home/sites/local.testsite.org/logs/access_log" common
    <Directory "/home/sites/local.testsite.org/www">
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    ServerName local.website.com
    DocumentRoot "/Users/john/Sites/local.website.com/www"
    <Directory "/Users/john/Sites/local.website.com/www">
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>
</VirtualHost>

ServerName local.testsite.org
DocumentRoot“/home/sites/local.testsite.org/www”
ErrorLog“/home/sites/local.testsite.org/logs/error\u log”
CustomLog“/home/sites/local.testsite.org/logs/access\u log”通用
允许超越所有
命令允许,拒绝
通融
我的正则表达式与之匹配():

/\n\t服务器名local.duudadou.com(.|\n)*/

如何将此表达式用于sed,我不知道如何使此表达式用于sed


sed'/\n\tServerName local.testsite.org(.|\n)*/'/usr/local/etc/httpd/extra/httpd vhosts.conf

我最终放弃了这种方法,使用一种更简单的方法在apache上为每个主机使用单独的.conf文件创建和删除虚拟主机。这样我就可以创建或删除一个文件,而不是在文件中搜索多行字符串

我想在Mac上为apache中的每个virtualhost加载一个单独的.conf文件(apache等都是用自制软件安装的)

这在我的httpd.conf中很容易设置。不要查找文件,只需使用
etc/httpd/vhosts/*.conf查找目录中的所有文件即可:

# Virtual hosts
# Include /usr/local/etc/httpd/extra/httpd-vhosts.conf  # Original rule
Include /usr/local/etc/httpd/vhosts/*.conf              # New rule
这将查找并包括该文件夹中的所有.conf文件。我的文件名如下:
local.website.com.conf
,包含如下内容:

<VirtualHost *:80>
    ServerName local.testsite.org
    DocumentRoot "/home/sites/local.testsite.org/www"
    ErrorLog "/home/sites/local.testsite.org/logs/error_log"
    CustomLog "/home/sites/local.testsite.org/logs/access_log" common
    <Directory "/home/sites/local.testsite.org/www">
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    ServerName local.website.com
    DocumentRoot "/Users/john/Sites/local.website.com/www"
    <Directory "/Users/john/Sites/local.website.com/www">
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>
</VirtualHost>

ServerName local.website.com
DocumentRoot“/Users/john/Sites/local.website.com/www”
允许超越所有
命令允许,拒绝
通融

使用这种方法,您只需删除一个.conf文件,重新启动apache,就完成了

这是因为sed不能自然地处理多行模式。有一种非常反直觉的方法可以让它工作,但老实说,最好的办法是使用perl。更多信息:您是否尝试过
-r
标志?例如,
sed-r's/x/y/g'$file
@Saichovsky-r返回带有
sed:invalize选项的值--r