切割 Apache 虚拟主机块
Cut Apache virtual host block
我正在尝试使用 bash 从包含大量虚拟主机的文件中剪切某些虚拟主机。
例如。在我的脚本中,我想获得使用 the_one_known2.example.com
的虚拟主机,但另一次是 the_one_known3.example.com
,即我想获得一些预先已知的 URL 形式的 apache 配置文件(存在于ServerName 或 ServerAlias)作为参数在 bash 脚本中设置。
<VirtualHost *:80 *:${SERVER_PORT}>
ServerName test1.example.com
ServerAlias test2.example.com
ServerAlias the_one_known1.example.com
ServerAlias test3.example.com
</VirtualHost>
<VirtualHost *:80 *:${SERVER_PORT}>
ServerName test4.example.com
ServerAlias the_one_known2.example.com
ServerAlias test5.example.com
ServerAlias test6.example.com
</VirtualHost>
<VirtualHost *:80 *:${SERVER_PORT}>
ServerName the_one_known3.example.com
ServerAlias test7.example.com
ServerAlias test8.example.com
ServerAlias test9.example.com
</VirtualHost>
所以我的 URL 变量将更改为例如。 the_one_known2.example.com
我会得到:
<VirtualHost *:80 *:${SERVER_PORT}>
ServerName test4.example.com
ServerAlias the_one_known2.example.com
ServerAlias test5.example.com
ServerAlias test6.example.com
</VirtualHost>
[编辑]
到目前为止,我试图找到所选虚拟主机的第一行:
url_line=$(grep -n -m 1 "${URL}" ./apache.conf" | sed 's/\([0-9]*\).*//')
vitual_host_start_line="$((app_line-1))" // this is an assumption that Virtual Host starts just one line before the first occurance of URL
echo $vitual_host_line // the place it starts
但是我找不到这个虚拟主机的最后一行,因为它是 </VirtualHost>
在 vitual_host_start_line
之后的第一次出现
与awk
:
awk -v name="the_one_known2.example.com" 'BEGIN{RS=ORS="</VirtualHost>\n"} [=10=]~name{print; exit}' file
我假设 the_one_known2.example.com
不是您文件中另一个域的子字符串。
参见:8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
使用您显示的示例,请尝试以下 awk
代码。在 GNU awk
.
中编写和测试
awk -v RS='<VirtualHost[^/]*/' -v ORS="VirtualHost>\n" 'RT~/[[:space:]]+the_one_known2\.example\.com\n/{print RT}' Input_file
解释: 简单的解释就是,使 RS(记录分隔符)从 <VirtualHost
到下一个 /
发生(非贪婪匹配)。然后检查它是否有 [[:space:]]+the_one_known2\.example\.com\n
如果是则打印匹配值,这将在段落结束时打印 VirtualHost>
因为它在程序中设置为 ORS 值。
我正在尝试使用 bash 从包含大量虚拟主机的文件中剪切某些虚拟主机。
例如。在我的脚本中,我想获得使用 the_one_known2.example.com
的虚拟主机,但另一次是 the_one_known3.example.com
,即我想获得一些预先已知的 URL 形式的 apache 配置文件(存在于ServerName 或 ServerAlias)作为参数在 bash 脚本中设置。
<VirtualHost *:80 *:${SERVER_PORT}>
ServerName test1.example.com
ServerAlias test2.example.com
ServerAlias the_one_known1.example.com
ServerAlias test3.example.com
</VirtualHost>
<VirtualHost *:80 *:${SERVER_PORT}>
ServerName test4.example.com
ServerAlias the_one_known2.example.com
ServerAlias test5.example.com
ServerAlias test6.example.com
</VirtualHost>
<VirtualHost *:80 *:${SERVER_PORT}>
ServerName the_one_known3.example.com
ServerAlias test7.example.com
ServerAlias test8.example.com
ServerAlias test9.example.com
</VirtualHost>
所以我的 URL 变量将更改为例如。 the_one_known2.example.com
我会得到:
<VirtualHost *:80 *:${SERVER_PORT}>
ServerName test4.example.com
ServerAlias the_one_known2.example.com
ServerAlias test5.example.com
ServerAlias test6.example.com
</VirtualHost>
[编辑] 到目前为止,我试图找到所选虚拟主机的第一行:
url_line=$(grep -n -m 1 "${URL}" ./apache.conf" | sed 's/\([0-9]*\).*//')
vitual_host_start_line="$((app_line-1))" // this is an assumption that Virtual Host starts just one line before the first occurance of URL
echo $vitual_host_line // the place it starts
但是我找不到这个虚拟主机的最后一行,因为它是 </VirtualHost>
在 vitual_host_start_line
与awk
:
awk -v name="the_one_known2.example.com" 'BEGIN{RS=ORS="</VirtualHost>\n"} [=10=]~name{print; exit}' file
我假设 the_one_known2.example.com
不是您文件中另一个域的子字符串。
参见:8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
使用您显示的示例,请尝试以下 awk
代码。在 GNU awk
.
awk -v RS='<VirtualHost[^/]*/' -v ORS="VirtualHost>\n" 'RT~/[[:space:]]+the_one_known2\.example\.com\n/{print RT}' Input_file
解释: 简单的解释就是,使 RS(记录分隔符)从 <VirtualHost
到下一个 /
发生(非贪婪匹配)。然后检查它是否有 [[:space:]]+the_one_known2\.example\.com\n
如果是则打印匹配值,这将在段落结束时打印 VirtualHost>
因为它在程序中设置为 ORS 值。