LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-05-2009, 08:16 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
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:FB3" 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:FB3" in that variable.
I have also created a new Variable say Var2 containing new mac address
say Var2=00:16:3E:64:FB3

Now how do i replace the value "mac=00:16:3E:64:FB3" 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
 
Old 11-05-2009, 10:46 AM   #2
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by anon237; 11-05-2009 at 03:14 PM. Reason: Added -i switch information
 
Old 11-05-2009, 05:38 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use awk instead
Code:
#!/bin/bash
awk 'BEGIN{ 
 newmac="00:16:3E:64:FB:3A"
 OFS=FS=","
}
/^vif /{
    $2="mac="newmac
}1' file
 
0 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed for sed command. pinga123 Linux - Newbie 2 11-06-2009 01:57 AM
sed help needed ZAMO Linux - General 7 01-09-2009 06:58 AM
sed help needed avijitp Programming 8 11-19-2007 08:33 AM
SED help needed DriveMeCrazy Linux - General 2 11-20-2006 10:04 PM
Sed/Awk command help needed. farmerjoe Programming 3 03-02-2005 11:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration