views:

57

answers:

3

hi! i have a proxy config file which has the following line:

Allow 212.21.3.44

i'd like to replace that ip address portion with my new ip address when it changes. it would probably be easier to just use the line number when searching for it - i don't think that config file will change at all other than that particular setting.

how can i do it from the command line with something like like perl, sed, etc?

thanks!

+1  A: 
sed -i 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/my.new.ip.here/' file.txt

The -i flag will change the file in place (depends on your distro/flavor of *nix - works on Ubuntu for me).

Vivin Paliath
And on most Linux box's you can add a '-i' option to `sed` to have it make the change to that file directly.
Kaleb Pederson
@Kaleb Didn't know about that! Thanks - updating my answer.
Vivin Paliath
seems dangerous to bulk change all ip addresses in the file to the new one, when the poster mentions one specific line
jack
evilclown, good catch! think i might go with Brandon's solution below for that reason
jack
@evilclown good point. I mistakenly assumed that it was the only line in the file.
Vivin Paliath
+3  A: 

If you want to use sed to change a specific line number, you could use:

sed -i '<line number> s/Allow .*$/Allow <new ip>/g' <filename>

Brandon Horsley
works great - thanks!
jack
A: 

in case line number does change

sed -i '/Allow/s/Allow .*$/Allow <new ip>/' <filename>
ghostdog74