Change specific ip address in a file

I need to change a line from a xen cfg file using sed if it's possible. The original line is:

vif         = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'ip=10.1.10.4,vifname=veth107a' ]

I want to change ONLY the IP address of the second part ==> ip=10.1.10.4 to another IP --> ip=192.222.11.6 The first one ip=123.456.789.123 keeps untouchable.

My new line shoud be like this now:

vif         = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'ip=192.222.11.6,vifname=veth107a' ]

Can you help me?

thanks in advance.
regards
Israel

Try this if the ip address occurs only once in the file:

sed 's/ip=10.1.10.4/ip=192.222.11.6/' file

Arrrr, this is a very very dirty hack:

$ cat myconf
vif         = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'ip=10.1.10.4,vifname=veth107a' ]
$ 
$ cat myconf | sed ''/ip=10.1.10.4,v/s//ip=192.222.11.6,v/'' > myconf
$ 
$ cat myconf
vif         = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'ip=192.222.11.6,vifname=veth107a' ]
$ 

opss, sorry guys...

I forgot to say about the value I want to change... ip=10.1.10.4 is a random ip, I just want to change the original random value to other random value too...

I mean, change the second part of value ip=un.known.ip.add to ip=other.ip.add.ress

Am I asking too much? :slight_smile:

regards
Israel.

i'd do it with perl:

perl -i -ne's/(ip=.+)(ip=.+?,)/$1ip=new.ip.address,/;print'

---------- Post updated at 02:13 PM ---------- Previous update was at 02:12 PM ----------

[/COLOR]sorry forgot to include the filename at the end:

perl -i -ne's/(ip=.+)(ip=.+?,)/$1ip=new.ip.address,/;print' file

very nice rugdog:

What if I need to remove that new ip entry I already changed the way you said? I want this output now:

vif         = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'vifname=veth107a' ]

really appreciate your help
regards
Israel.

that would be:

perl -i -ne's/(ip=.+)(ip=.+?,)/$1/;print' file

be aware that the "-i" option is the in-place edit option of perl, that means that the file is modified right away when you run the script.

umm.. rugdog.. I'm trying to use a variable instead of value in ip=xx.xx.xx.x ... I mean,

VAR=12.12.12.12.

perl -i -ne's/(ip=.+)(ip=.+?,)/$1ip=$VAR,/;print' file

How perl treat Variables inside this code?

thanks again :slight_smile:
regards
Israel.

---------- Post updated at 03:05 PM ---------- Previous update was at 02:57 PM ----------

:slight_smile: Promise the last question

You told me how to remove ip=xx.xx.xx.xx string.. it works perfectly.. but, what if I want to add it again?

Original line:

vif = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'vifname=veth107a' ]

Desire line:

vif = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'ip=xx.xx.xx.xx,vifname=veth107a' ]

thanks tons
sorry to bother you again
regards,

israel

#!/bin/bash

if [ $# -lt 2 ]; then
    echo
    echo "Usage - $(basename $0) FILE IP"
    echo "  FILE is input filename"
    echo "  IP is new IP address"
    echo
    exit 0
fi

sed "s/\(, 'ip=\)[^,]\+,/\1$2,/" $1

in the case of substituting a env var, do an export of the var, before the script:

export VAR=12.12.12.12.

perl -i -ne's/(ip=.+)(ip=.+?,)/$1ip=$ENV{VAR},/;print' file

as for adding back

perl -i -ne's/(ip=.+vifname=.+)(vifname=.+)/$1ip=$ENV{VAR},$2/;print' file