3

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?.*)/gm

You can try test online with https://regex101.com/ and it works great replacing with:

\1\2http-proxy-port=9000

I 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/servers

How 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 = defaultpassword

Expected output will be

...
[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
  • Expected out in post above – ccolina Dec 17 '16 at 18:17
2

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
}' infile

This 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.

  • I dont know how it works, but the script make the job. Thanks – ccolina Dec 17 '16 at 23:34
  • You know how use to ignore no starting [.*]? – ccolina Dec 21 '16 at 17:29
  • You know how use to ignore no starting [.*]? Sample: [global] http-proxy-exceptions = .vtr.com,.vtr.com,.vtr.cl,.vtr.cl,127.0.0.1,localhost,::1,127.*,[::1] http-proxy-host = proxy.vtr.cl http-proxy-port = 8080 # http-proxy-username = defaultusername In this case sed no word because [::1] break the search – ccolina Dec 21 '16 at 17:31
  • @ccolina - see edit - use ^ to anchor to beginning of line: sed '/^\[.*\]/h... – don_crissti Dec 21 '16 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.