ram*_*tta 3 unix linux shell awk sed
我正在寻找一种方法来根据给定的模式在 {} 之间 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";
}
}
Run Code Online (Sandbox Code Playgroud)
如果传递给脚本的参数是 "dev" ,那么它应该匹配模式位置/dev 并将下面的块提取到 shell 变量中:
位置/开发{
Run Code Online (Sandbox Code Playgroud)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
Run Code Online (Sandbox Code Playgroud)
结果:
$ 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
Run Code Online (Sandbox Code Playgroud)
我们可以单独完成awk,请您尝试跟随。
awk '/}/ && found{exit} /location \/dev/{found=1;next} found && NF' Input_file
Run Code Online (Sandbox Code Playgroud)
由于 OP 提到只应打印第 1 组,因此我使用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.
Run Code Online (Sandbox Code Playgroud)
输出如下。
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";
Run Code Online (Sandbox Code Playgroud)