我有一个配置文件(nginx default.conf),我有这种字符串:
server {
location / {
proxy_set_header Host sudomain1.somewhere.com;
proxy_pass http://127.0.0.1:8999/;
}
}
server {
location / {
proxy_set_header Host sudomain2.somewhere.com;
proxy_pass http://127.0.0.1:8755/;
}
}
server {
location / {
proxy_set_header Host sudomain3.somewhere.com;
proxy_pass http://127.0.0.1:8525/;
}
}
如何减去有关subdomain2的配置?
server {
location / {
proxy_set_header Host sudomain2.somewhere.com;
proxy_pass http://127.0.0.1:8755/;
}
}
如果有可能,使用grep会很好! 谢谢你的宝贵帮助!
答案 0 :(得分:3)
您可以使用此gnu awk命令使用自定义记录分隔符(subdomain2
)获取与RS
匹配的块:
awk -v RS='}[[:space:]]*}[[:space:]]*' '/sudomain2/{printf $0 RT}' default.conf
server {
location / {
proxy_set_header Host sudomain2.somewhere.com;
proxy_pass http://127.0.0.1:8755/;
}
}
编辑:打印除subdomain2
以外的其他块:
awk -v RS='}[[:space:]]*}[[:space:]]*' '!/sudomain2/{printf $0 RT}' file
server {
location / {
proxy_set_header Host sudomain1.somewhere.com;
proxy_pass http://127.0.0.1:8999/;
}
}
server {
location / {
proxy_set_header Host sudomain3.somewhere.com;
proxy_pass http://127.0.0.1:8525/;
}
}