Using command line tools, I am trying to find any ip address except 127.0.0.1 and replace with a new ip. I tried using sed
:
sed 's/\([0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}\)\(?!127.0.0.1\)/'$ip'/g' file
Please can you help me?
Since sed won't support negative lookahead assertion, i suggest you to use Perl instead of sed.
perl -pe 's/\b(?:(?!127\.0\.0\.1)\d{1,3}(?:\.\d{1,3}){3})\b/'"$ip"'/g' file
Example:
$ cat file
122.54.23.121
127.0.0.1 125.54.23.125
$ ip="101.155.155.155"
$ perl -pe 's/\b(?:(?!127\.0\.0\.1)\d{1,3}(?:\.\d{1,3}){3})\b/'"$ip"'/g' file
101.155.155.155
127.0.0.1 101.155.155.155
Hacky one through the PCRE verb (*SKIP)(*F)
,
$ perl -pe 's/\b127\.0\.0\.1\b(*SKIP)(*F)|\b\d{1,3}(?:\.\d{1,3}){3}\b/'"$ip"'/g' file
101.155.155.155
127.0.0.1 101.155.155.155
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With