LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed command help needed. (https://www.linuxquestions.org/questions/linux-newbie-8/sed-command-help-needed-767055/)

pinga123 11-05-2009 08:16 AM

sed command help needed.
 
Code:

vif = ['bridge=xenbr0,mac=00:16:3E:64:FB:D3,type=ioemu']
I need to replace "00:16:3E:64:FB:D3" to a new mac address value from below mentioned file.
Code:

[root@OVM-SERVER1 vm_temp]# cat vm.cfg
acpi = 1
apic = 1
builder = 'hvm'
device_model = '/usr/lib/xen/bin/qemu-dm'
disk = ['file:/var/ovs/mount/93B14928C7A6438284753B2F2AB197BB/seed_pool/vm_temp/System.img,hda,w',
'file:/OVS/iso_pool/winxpsp2/WXPVOL_EN.iso,hdc:cdrom,r',
]
kernel = '/usr/lib/xen/boot/hvmloader'
memory = '300'
name = 'vm_temp'
on_crash = 'restart'
on_reboot = 'restart'
pae = 1
serial = 'pty'
timer_mode = '0'
usbdevice = 'tablet'
uuid = '8569c556-fdf9-41f7-93f4-52350560b13c'
vcpus = 1
vif = ['bridge=xenbr0,mac=00:16:3E:64:FB:D3,type=ioemu']
vif_other_config = []
vnc = 1
vncconsole = 1
vnclisten = '0.0.0.0'
vncpasswd = 'oracle'
vncunused = 1

I have created a variable say Var1 and stored value "mac=00:16:3E:64:FB:D3" in that variable.
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}'`
echo "vif is found at $Number3"
Variable=`grep -n '^vif =' vm.cfg |awk -F"," '{print $2}'`
echo $Variable

sed "s\$Variable\mac=$NewMacAddress\" vm.cfg


druuna 11-05-2009 10:46 AM

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.

ghostdog74 11-05-2009 05:38 PM

use awk instead
Code:

#!/bin/bash
awk 'BEGIN{
 newmac="00:16:3E:64:FB:3A"
 OFS=FS=","
}
/^vif /{
    $2="mac="newmac
}1' file



All times are GMT -5. The time now is 06:55 AM.