Home > Blockchain >  sed replacement for no_proxy cuts number in localhost
sed replacement for no_proxy cuts number in localhost

Time:12-28

I have a no_proxy string I'd like to modify to fit with Java Opts.

❯ export no_proxy_test=localhost,127.0.0.1,.myfancy.domain,.myotherfancy.domain
❯ echo $no_proxy_test
localhost,127.0.0.1,.myfancy.domain,.myotherfancy.domain

I need for each value to be delimited with pipes "|" instead and I need to remove the dots for the wildcard. So should be:

localhost|127.0.0.1|myfancy.domain|myotherfancy.domain

When I use:

❯ echo $no_proxy_test | sed 's/,./|/g'
localhost|27.0.0.1|myfancy.domain|myotherfancy.domain

For some reason it cuts the 1 in 127.0.0.1 and I don't understand why? I thought I may achieve it with a double sed:

❯ echo $no_proxy_test | sed 's/,/|/g' | sed 's/|./|/g'
localhost|27.0.0.1|myfancy.domain|myotherfancy.domain

But same problem there. Does anyone have an idea? I don't want to sed replace 27 with 127. Would be interesting if I ran into a bug or if anyone could explain why 1 is being cut from the string.

Thank you!

CodePudding user response:

You can use

sed -E 's/,\.?/|/g'    # POSIX ERE
sed 's/,\.\{0,1\}/|/g' # POSIX BRE

This will replace a comma followed with an optional . char with a pipe symbol.

See the online demo:

#!/bin/bash
s='localhost,127.0.0.1,.myfancy.domain,.myotherfancy.domain'
sed 's/,\.\{0,1\}/|/g' <<< "$s"
# => localhost|127.0.0.1|myfancy.domain|myotherfancy.domain
  • Related