Using sed to Search & Replace IP Address
Hello.
I would like to use sed to search and replace all occurrences of a specific IP address within a configuration file. To illustrate what I would like to do: currIP=10.2.123.1 newIP=10.2.123.3 Config File: some text 10.2.123.10 some text 10.2.123.1 more text I am struggling with a sed command (using regex) that replaces "10.2.123.1" with "10.2.123.3" and does NOT change "10.2.123.10" to "10.2.123.30". Any advice/help on this would be GREATLY appreciated. Thanks, CHuey |
The following command worked on the example you provided:
Code:
echo "some text 10.2.123.10 some text 10.2.123.1" | sed 's@\b10\.2\.123\.1\b@10.2.123.3@g' |
\b matches between a word character and a non-word character. sed "s/\(10\.2\.123\.\)1\b/\13/g" works.
E: Ninja’d |
The following worked:
sed -i -e 's/'$currip'\b/'$newip'/g' Thank you VERY MUCH for helping me on this. Regards, CHuey |
Try just using a single set of double quotes around the expression.
Code:
sed -i -e "s/$currip\b/$newip/g" And please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. |
Quote:
For example, suppose: currip=10.2.123.1 newip=10.2.123.3 And your file contains: some text 210.2.123.1 some text 10.2.123.1 Your existing command: Code:
echo "some text 210.2.123.1 some text 10.2.123.1" | sed 's/'$currip'\b/'$newip'/g' EDIT: To be 200% safe, you also need to make sure that the dots in your currip variable are escaped to prevent sed from treating them as wildcards. |
Thank you for the advise... it is definitely better to be 100+% safe.
I have another question: What regex should I use if the IP address is not on a word boundary. For example: currIP=10.2.123.1 newIP=10.2.123.3 Config File: line 1: some text 10.2.123.10 more text 10.2.123.1 line 2: source s_ext { tcp(ip(10.2.123.1) port (8514)); }; I need to generate a sed command to replace "10.2.123.1" with "10.2.123.3" in lines 1 & 2 and NOT change "10.2.123.10" to "10.2.123.30" in line 1. Is it possible to use the same sed command to cover both lines in the Config File above? |
The same command works for me. For instance:
Code:
$ currip="10\\.2\\.123\\.1" |
Thank you Dark_Helmet... it did work!!! I was not familiar with the "@" usage, so I learned something very valuable.
Regards, CHuey |
Just to add in case someone finds this thread through Google...
sed's substitution command can use different characters to delimit the search and replace portions of the substitution. The '@' is no different than '/' in sed's eyes--they are functionally equivalent. The reason I use the @ by default is because I use a lot of escape sequences in my regexs and sometimes use path components. For my purposes, using the forward slash could make for some ugly, unreadable commands. For instance, to adjust a path of /home/fred/ or /home/barney/ to be /home/backup/fred/ or /hom/backup/barney/ respectively: Code:
echo "/home/fred/" | sed "s/\/home\/\(fred\|barney\)\//\/home\/backup\/\1\//" Code:
echo "/home/fred/" | sed "s@/home/\(fred\|barney\)/@/home/backup/\1/@" |
|
All times are GMT -5. The time now is 11:01 AM. |