我正在寻找一种基于给定模式对{}之间的线段进行grep的方法。我尝试了在google中找到的各种模式,但这些模式都无济于事。我在regex中不是专业人士。解决这个问题。
这是示例源文件:
Data {
status 400;
server_name test.dummy.com;
location /test {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_read_timeout 5m;
proxy_send_timeout 5m;
proxy_pass http://xyz.9201.com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /dev {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_read_timeout 5m;
proxy_send_timeout 5m;
proxy_pass http://xyz.9202.com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /prd {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_read_timeout 5m;
proxy_send_timeout 5m;
proxy_pass http://xyz.9203.com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
如果传递给脚本的参数为“dev”,则应匹配位置/dev模式,并将以下block提取到shell变量中:
位置/dev {
我尝试了各种sed/awk命令模式,但是下面的这个给了我一些最接近的结果。
awk '/dev/{print}' RS={ FS=} test.conf
结果:
$ awk '/dev/{print}' RS={ FS=} test.txt
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_read_timeout 5m;
proxy_send_timeout 5m;
proxy_pass http://xyz.9201.com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /dev
最佳答案
我们可以在一个awk
本身中完成此操作,请尝试以下操作。
awk '/}/ && found{exit} /location \/dev/{found=1;next} found && NF' Input_file
由于OP提到只应打印第一套,所以我在这里使用
exit
在拳套打印后立即退出。说明:添加以上代码的说明。
awk ' ##Starting awk program from here.
/}/ && found{ ##Checking condition if a line contains } AND variable found is SET then do following.
exit ##Exiting from program here.
} ##Closing BLOCK for above condition here.
/location \/dev/{ ##Checking condition here if a line contains location /dev then do following.
found=1 ##Setting variable found to 1 here.
next ##next will skip all further statements from here.
} ##Closing BLOCK for above condition here.
found && NF ##Checking condition is found is SET and NF is NOT NULL then print current line.
' Input_file ##Mentioning Input_file name here.
输出如下。
proxy_set_header X-Forwarded-Host ;
proxy_set_header X-Forwarded-Server ;
proxy_set_header X-Forwarded-For ;
proxy_set_header Host ;
proxy_read_timeout 5m;
proxy_send_timeout 5m;
proxy_pass http://xyz.9202.com;
proxy_http_version 1.1;
proxy_set_header Upgrade ;
proxy_set_header Connection "upgrade";
关于linux - 根据匹配模式将一行代码grep到shell变量中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59218263/