您只需更改/test/v2.0.0块中的端口:
server {
location /test/v2.0.3 {
modsecurity on;
proxy_pass http://10.1.0.6:3000;
}
location /test/v2.0.0 {
modsecurity on;
proxy_pass http://10.1.0.6:3000;
}
}
sed '0,/:[0-9].*;/{s/:[0-9].*;/:5555;/}' test.nginx
- 改变第一场比赛
sed '/.*location.*\/test\/v2.0.0\/.*:[0-9].*;/{s/:[0-9].*;/:5555;/}' test.nginx
- 不会改变任何东西
sed 's/.*location.*\/test\/v2.0.0\/.*:[0-9].*;/:5555;/' test.nginx
- 不会改变任何东西
附注:
任务听起来像什么: 找到位置
/test/v2.0.0.0
前面有除 #
之外的任何字符,选择括号 {
}
中的所有内容,找到端口字符串 :3000
;之间,替换为指定的。
输出:
server {
location /test/v2.0.3 {
modsecurity on;
proxy_pass http://10.1.0.6:3000;
}
location /test/v2.0.0 {
modsecurity on;
proxy_pass http://10.1.0.6:5555;
}
}
您可以使用以下
awk
来实现您的结果。
awk '
/location \/test\/v2.0.0/ {flag=1}
flag && /proxy_pass/ {sub(/:[0-9]+;/, ":5555;")}
/}/ {flag=0}
{print}
' your_file
该命令将:
flag
的行时,设置 /test/v2.0.0
。proxy_pass
,它将用 5555
替换端口号。}
)时,flag
将被取消设置,并且将打印每一行。