Reputation: 1
I'm having some problems with the following expression from a bash script:
ipreg=^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$
sed -i 's/\"https:\/\/'$ipreg':9999\",/\"https:\/\/'${newip}':9999\",/g' /usr/share/config.file
Sed can't find the old Ip address with the var ipreg, but if i put in the exact address it does get replaced with the new IP.
Upvotes: 0
Views: 102
Reputation: 556
This could work, taking into account the comment of tripleee.
$ cat config.file
.. / "https://10.10.10.10:9999";, /* @scratch /configuration/config.js/5 * * ==== default_route *
$ cat changeip.sh
#!/bin/bash
ipreg='[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
newip='127.0.0.1'
sed -ri 's/"https:\/\/'"${ipreg}"':9999"/"https:\/\/'"${newip}"':9999"/' "$1"
cat "$1"
$ ./changeip.sh config.file
.. / "https://127.0.0.1:9999";, /* @scratch /configuration/config.js/5 * * ==== default_route *
Does the IP occur more than once? Only than you will need the global option.
Upvotes: 0
Reputation: 1118
I think you are making this too complex. I don't see a reason to match IPv4 addresses so tightly.
Why not simplify it a bit like this:
sed -i "s|https://[0-9.]*:9999|https://$newip:9999|" /usr/share/config.file
Also, you probably don't want anchors in your regex, since it's in the middle of a string. ^
matches the beginning of a line and $
matches the end - you don't want those in the middle of your match pattern.
Upvotes: 2
Reputation: 189387
Many sed
dialects do not support {1,3}
repetition. Try with backslashes before the curlies, and/or see if your sed
has an -r
or -E
option to enable Extended Regular Expression (ERE) syntax.
In the worst case, just write the repetitions in longhand, or maybe simplify them to just *
if you can live with the rather remote possibility for false positives.
As an aside, you should use quotes aroind anything which contains shell metacharacters.
Upvotes: 0