我有 subversion 配置文件 ( ~/.subversion/servers
)
我需要修改以添加代理信息(主机、端口、例外)。该文件包含许多带有代理信息的部分。我只想修改[全球的]。
我已经为此创建了一个正则表达式,但它不起作用。
/(\[global\].*[\n])((.*[\n])*)([\s\#]*http-proxy-port\s?=\s?.*)/gm
您可以尝试在线测试https://regex101.com/它非常适合替换为:
\1\2http-proxy-port=9000
我尝试sed
执行上面的行,但没有任何提示。
sed -i -r 's/(\[global].*[\n])((.*[\n])*)([\s\#]*http-proxy-port\s?=\s?.*)/\1\2http-proxy-port=9000/gm' \
~/.subversion/servers
我怎样才能使用sed
上面的正则表达式?
此示例颠覆文件:
### The currently defined server options are:
### http-proxy-host Proxy host for HTTP connection
### http-proxy-port Port number of proxy host service
### http-proxy-username Username for auth to proxy service
### http-proxy-password Password for auth to proxy service
### http-proxy-exceptions List of sites that do not use proxy
### http-timeout Timeout for HTTP requests in seconds
[groups]
# group1 = *.collab.net
# othergroup = repository.blarggitywhoomph.com
# thirdgroup = *.example.com
### Information for the first group:
# [group1]
# http-proxy-host = proxy1.some-domain-name.com
# http-proxy-port = 80
# http-proxy-username = blah
# http-proxy-password = doubleblah
# http-timeout = 60
### Information for the second group:
# [othergroup]
# http-proxy-host = proxy2.some-domain-name.com
# http-proxy-port = 9000
### SSL certificate. See details above for overriding security
### due to SSL.
[global]
# http-proxy-exceptions = *.domain.org, *.domain.com
# http-proxy-host = proxy.domain.com
# http-proxy-port = 8080
# http-proxy-username = defaultusername
# http-proxy-password = defaultpassword
预期输出将是
...
[global]
http-proxy-exceptions = *.otherdomain.org, *.otherdomain.com, 127.0.0.1, localhost
http-proxy-host = proxy.otherdomain.com
http-proxy-port = 9000
# http-proxy-username = defaultusername
# http-proxy-password = defaultpassword
答案1
正如建议的那样,有更好的方法来编辑 INI 文件...
不过,这是一种方法sed
:
sed '/^\[.*\]/h
/http-proxy-exceptions/{x;/\[global\]/!{x;b;};x;c\
http-proxy-exceptions = *.otherdomain.org, *.otherdomain.com, 127.0.0.1, localhost
}
/http-proxy-host/{x;/\[global\]/!{x;b;};x;c\
http-proxy-host = proxy.otherdomain.com
}
/http-proxy-port/{x;/\[global\]/!{x;b;};x;c\
http-proxy-port = 9000
}' infile
每次遇到行匹配时,都会用模式空间内容覆盖保持缓冲区[.*]
(即将每个节名称保存到h
旧缓冲区中)。在http-.*
与您的模式匹配的每一行上x
更改缓冲区 - 如果保留空间确实如此不是( !
) 匹配[global]
,然后它x
变回并通过 跳到下一个循环b
。如果保持空间匹配,则[global]
它x
会变回并c
挂起模式空间的内容。