gpt4 book ai didi

linux - 根据匹配模式将一行行 grep 到 shell 变量中

转载 作者:行者123 更新时间:2023-12-03 17:43:43 25 4
gpt4 key购买 nike

我正在寻找一种基于给定模式在 {} 之间 grep 行块的方法。我尝试了我在谷歌中找到的各种模式,但没有一个对我的案例有帮助。我不是正则表达式的专家。寻找一些帮助来解决这个问题。
这是示例源文件:

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 并将以下块提取到 shell 变量中:

位置/开发{

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";


我尝试了各种 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com