LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 06-13-2010, 11:26 PM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
formatting help needed.


How would i write a script which will add a following content to a file.

File Before running script.
Code:
acpi = 1
apic = 1
builder = 'hvm'
device_model = '/usr/lib/xen/bin/qemu-dm'
disk = ['file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/running_pool/IFLMUD5IM0196/System-hdc.img,hda,w',
',hdc:cdrom,r',
]
File After running the script.

Code:
acpi = 1
apic = 1
builder = 'hvm'
device_model = '/usr/lib/xen/bin/qemu-dm'
disk = ['file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/running_pool/IFLMUD5IM0196/System-hdc.img,hda,w',
'file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/iso_pool/WindowXP/XpBoot.iso,hdc:cdrom,r',
]
Here i have added file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/iso_pool/WindowXP/XpBoot.iso
file.

There fore if i want to rerun the script with another value it should remove the previous entry and enter the new entry.
as below.

File Before running script.

Code:
acpi = 1
apic = 1
builder = 'hvm'
device_model = '/usr/lib/xen/bin/qemu-dm'
disk = ['file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/running_pool/IFLMUD5IM0196/System-hdc.img,hda,w',
'file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/iso_pool/WindowXP/XpBoot.iso,hdc:cdrom,r',
]
File After running the script.

Code:
acpi = 1
apic = 1
builder = 'hvm'
device_model = '/usr/lib/xen/bin/qemu-dm'
disk = ['file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/running_pool/IFLMUD5IM0196/System-hdc.img,hda,w',
'file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/iso_pool/Window2003/2k3.iso,hdc:cdrom,r',
]
Being a new to unix scripting this task is a big challenge for me and i could not able to solve it.
therefore seeking your help here.
 
Old 06-14-2010, 12:20 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
How would you have done it in a non-unix scripting environment?

Maybe we can teach you by pointing out the Linux equivalents
of whatever you ARE familiar with.


Cheers,
Tink
 
Old 06-23-2010, 06:20 AM   #3
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
I have tried with following code but still no success . Please help.

Code:
Linenumber=`grep -n "hdc:cdrom" /usr/local/sbin/Test/$VM_GUEST/vm.cfg | cut -f1 -d:`

echo $Linenumber
echo "hdc is found at '$Linenumber'"

sed "${Linenumber}d" /usr/local/sbin/Test/$VM_GUEST/vm.cfg > /usr/local/sbin/Test/$VM_GUEST/tmp.cfg
mv /usr/local/sbin/Test/$VM_GUEST/tmp.cfg /usr/local/sbin/Test/$VM_GUEST/vm.cfg

echo "After removing line number"

cat /usr/local/sbin/Test/$VM_GUEST/vm.cfg

CD_VALUE="'$CD_PATH,hdc:cdrom,r',"
awk -v m=$CD_VALUE 'NR=='$Linenumber' {print m}1' /usr/local/sbin/Test/$VM_GUEST/vm.cfg > /usr/local/sbin/Test/$VM_GUEST/tmp.cfg
mv /usr/local/sbin/Test/$VM_GUEST/tmp.cfg /usr/local/sbin/Test/$VM_GUEST/vm.cfg
cat /usr/local/sbin/Test/$VM_GUEST/vm.cfg
 
Old 06-23-2010, 06:56 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well, your script works for me, provided you previously assign the variable CD_PATH. Do you get some error messages? What is the content of /usr/local/sbin/Test/$VM_GUEST/vm.cfg after running this script?

Anyway, so many passes are not necessary if sed can do it in one line. See the following example (directly on command line):
Code:
$ cat file0
acpi = 1
apic = 1
builder = 'hvm'
device_model = '/usr/lib/xen/bin/qemu-dm'
disk = ['file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/running_pool/IFLMUD5IM0196/System-hdc.img,hda,w',
',hdc:cdrom,r',
]
$ CD_PATH=file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/iso_pool/WindowXP/XpBoot.iso
$ sed -i.bck "s%'.*\(,hdc:cdrom,r',\)%'$CD_PATH\1%" file0
$ cat file0
acpi = 1
apic = 1
builder = 'hvm'
device_model = '/usr/lib/xen/bin/qemu-dm'
disk = ['file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/running_pool/IFLMUD5IM0196/System-hdc.img,hda,w',
'file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/iso_pool/WindowXP/XpBoot.iso,hdc:cdrom,r',
]
$ CD_PATH=file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/iso_pool/Window2003/2k3.iso
$ sed -i.bck "s%'.*\(,hdc:cdrom,r',\)%'$CD_PATH\1%" file0
$ cat file0
acpi = 1
apic = 1
builder = 'hvm'
device_model = '/usr/lib/xen/bin/qemu-dm'
disk = ['file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/running_pool/IFLMUD5IM0196/System-hdc.img,hda,w',
'file:/var/ovs/mount/825D2FF7ED2645428AD5AED714B5E777/iso_pool/Window2003/2k3.iso,hdc:cdrom,r',
]
$
Note the -i option of sed: it make sed to edit the file in place, without intervention of temporary files. The -i.bck form automatically does a backup file adding the suffix .bck to the original name. The suffix can be anything at your choice. HTH.
 
  


Reply



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
[SOLVED] Little formatting help needed. pinga123 Linux - Newbie 7 03-23-2010 08:33 AM
Output formatting help needed. pinga123 Linux - Newbie 8 11-03-2009 06:34 AM
XGL needed for Beryl is conflicting with OpenGL needed for Cedega chunkeydelight Linux - Desktop 2 07-18-2007 05:50 AM
Errors installing Q, which is needed for Lex, which is needed for PHP Virtuality Linux - Software 1 05-29-2007 04:47 PM
Help needed in formatting an USB Harddrive vasanthd Linux - Laptop and Netbook 4 08-11-2005 10:25 AM

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

All times are GMT -5. The time now is 12:51 AM.

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