Hi,
I personally think your initial concept is to complicated.
You only need to know which line number the '...,mac=....' has (Numbers3 holds that value) if you want to show it as "informative" output. Both the filling of Numbers3 and Variable are not needed to change the content of vm.cfg
This is all you need:
Code:
#!/bin/bash
NewMacAddress="00:11:22:33:44:55"
sed "s/\(.*,mac=\).*\(,type=.*\)/\1$NewMacAddress\2/" vm.cfg
I'm not sure how much you know about sed, but the line above is a substitution (sed 's/<lookfor>/<replacewith>/' infile) command that uses backreferencing (the \(...\) and \1 \2 parts.
The <lookfor> part can be broken into 3 parts:
\(.*,mac=\) -> everything up to and including
,mac=. This is stored (keeping it simple) in \1.
\(,type=.*\) -> everything from
,type= to the end of the line. This is stored in \2
The .* now holds the mac address (the address only, not the mac= part). This is the part you want to replace with the new mac address.
The \( and \) tell sed that all in between should be stored. First in \1, second in \2 etc.
The <replacewith> puts it all back together:
\1 is replaced with the stored content (
vif = ['bridge=xenbr0,mac= in your example)
$NewMacAddress is replaced with the new mac address
\2 is replaced by its stored content (
,type=ioemu'] in your example)
This will print the line number and uses it to point sed immediately to the correct line in the vm.cfg file:
Code:
#!/bin/bash
Number3="`sed -n '/vif =/=' vm.cfg`"
echo "vif is found at $Number3"
NewMacAddress="00:11:22:33:44:55"
sed "19s/\(.*,mac=\).*\(,type=.*\)/\1$NewMacAddress\2/" vm.cfg
I do assume you gave a relevant example and there are no extra entries that allow for false hits.......
PS: Add the -i switch to change it in place instead of only outputting it to screen, the .bak part makes a backup with .bak as extension.
sed -i.bak "s/\(.*,mac=\).*\(,type=.*\)/\1$NewMacAddress\2/" vm.cfg
Hope this helps.