sed command help needed.
Code:
vif = ['bridge=xenbr0,mac=00:16:3E:64:FB:D3,type=ioemu'] Code:
[root@OVM-SERVER1 vm_temp]# cat vm.cfg I have also created a new Variable say Var2 containing new mac address say Var2=00:16:3E:64:FB:D3 Now how do i replace the value "mac=00:16:3E:64:FB:D3" to "mac=Var2" I have created following script but it is not working kindly help Code:
Number3=`grep -n '^vif =' vm.cfg |awk -F":" '{print $1}'` |
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 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 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. |
use awk instead
Code:
#!/bin/bash |
All times are GMT -5. The time now is 06:55 AM. |