我需要一个工具或单行或其他任何东西,它将导出server {...}
包含给定域的整个块。
举些例子:
### 1)
cat conf.d/domains.conf
server {
include http.conf;
server_name example.com;
# [...]
}
server {
include https.conf;
server_name foo.com;
# [...]
}
server {
include https.conf;
server_name example.com;
# [...]
}
---
./script conf.d/domains.conf example.com
server {
include http.conf;
server_name example.com;
# [...]
}
server {
include https.conf;
server_name example.com;
# [...]
}
### 2)
cat conf.d/domains.conf
server {
include ssl.conf;
server_name example-foo.com;
# [...]
}
server {
include ssl.conf;
server_name example-bar.com
example.com
fake.domain.com;
location / {
return 204 "OK";
}
# [...]
}
server {
# include headers.conf;
include ssl.conf;
server_name example-baz.com example.com;
# [...]
}
server {
include ssl.conf;
root /var/www/;
server_name example-baz.com exampleee.com;
# [...]
}
---
./script conf.d/domains.conf example.com
server {
include ssl.conf;
server_name example-bar.com
example.com;
# [...]
}
server {
include ssl.conf;
server_name example-bar.com
example.com
fake.domain.com;
location / {
return 204 "OK";
}
# [...]
}
server {
# include headers.conf;
include ssl.conf;
server_name example-baz.com example.com;
# [...]
}
我的配置有很多行,并且分为许多外部文件。
您知道执行此操作的工具吗?或者有什么建议吗?
谢谢!
答案1
您可以使用 awk 通过过滤分隔符之间的两个模式来实现此目的,在您的情况下是域和大括号 '$domain {'
例如:
awk '/exampledomain {/,/}/' exampledomains.conf
此命令将采用第一个分隔符:
示例域 {
和第二个分隔符,右花括号:
}
并打印出您选择的文件中的内容,包括域行,在此示例中:exampledomains.conf
请考虑输入必须是文字才能正常工作,包括空格。