changing line on a file

Hi, I want to change on a file this line:

vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14' ]

into:

vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14,vifname=veth216','ip=10.1.1.1,mac=00:16:3E:FD:58:BB,vifname=veth216a']

But there are some conditionals:

  1. last part '] of the original line after 00:16:3E:CE:23:14' ] may vary to 00:16:3E:CE:23:14' ] or 00:16:3E:CE:23:14 ' ]
  2. The second mac adrress of the line I need, 00:16:3E:FD:58:BB, must be different every time I run sed. Maybe just changing the last two characters BB to other two characters.
  3. The [ ' and ' ] of both lines could be separated by some espaces, example [ ' or [' or ' ] or '] so you should not take this characters as a reference.
  4. The Ip 167.xx.xx.xx and first mac address 00:16:3E:CE:23:14 are the same in two line. The second ip always is 10.1.1.1, and the second mac address must change every time I run sed command. :frowning:

thanks tons
regards,
Israel.

what have you tried till now??

Sorry, but I'm still with the problem... As the source file change, I mean the position of ' ] or [ ', so I'm not able to change to the output I want. I've tried a couple of sed commands.

regards,
Israel.

Hi Israel, would this work for you?
(ksh/bash)

rndhex() {
  echo "ibase=10;obase=16; $((RANDOM%256))"|bc
}

while read line; do
  case $line in
    vif*)   mac=${line##*mac=}
            mac="${mac%\'*]}"
            randomhex=CC
            newmac=${mac%:*:*:*}:$(rndhex):$(rndhex):$(rndhex)
            echo "${line%\' ]},vifname=veth216','ip=10.1.1.1,mac=$newmac,vifname=veth216a' ]" ;;
    *)      echo $line ;;
  esac
done<infile
$> cat infile
vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14' ]
$> ./test
vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14,vifname=veth216','ip=10.1.1.1,mac=00:16:3E:E8:3B:8E,vifname=veth216a' ]
$> ./test
vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14,vifname=veth216','ip=10.1.1.1,mac=00:16:3E:F3:46:99,vifname=veth216a' ]
$> ./test
vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14,vifname=veth216','ip=10.1.1.1,mac=00:16:3E:FE:51:A3,vifname=veth216a' ]

Hi Scrutinizer,

The script works perfectly :slight_smile:
Thanks tons

regards,
Israel