I have the subversion config file (~/.subversion/servers)I need modify to add proxy information (host, port, exceptions). This file contains many sections with proxy information. I only want to modify [global].I have already created a regular expression for this but it does not work./(\[global\].*[\n])((.*[\n])*)([\s\#]*http-proxy-port\s?=\s?.*)/gmYou can try test online with https://regex101.com/ and it works great replacing with:\1\2http-proxy-port=9000I try with sed executing the line above and no word.sed -i -r 's/(\[global].*[\n])((.*[\n])*)([\s\#]*http-proxy-port\s?=\s?.*)/\1\2http-proxy-port=9000/gm' \ ~/.subversion/serversHow can I make sed work with the above regex ?This sample subversion file:### 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 = defaultpasswordExpected output will be...[global]http-proxy-exceptions = *.otherdomain.org, *.otherdomain.com, 127.0.0.1, localhosthttp-proxy-host = proxy.otherdomain.comhttp-proxy-port = 9000# http-proxy-username = defaultusername# http-proxy-password = defaultpassword
Edit several lines from a certain section of an INI file
text processing;sed;regular expression
As suggested, there are better ways to edit INI files...Still, here's one way with 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}' infileThis overwrites the hold buffer with the pattern space content each time it encounters an line matching [.*] (i.e. it saves each section name into the hold buffer). On  each line that matches your http-.* patterns it exchanges buffers - if the hold space does not (!) match [global] then it exchanges back and skips to next cycle via b. If the hold space matches [global] it exchanges back and changes the content of the pattern space.