LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   formatting help needed. (https://www.linuxquestions.org/questions/linux-newbie-8/formatting-help-needed-813973/)

pinga123 06-13-2010 11:26 PM

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.

Tinkster 06-14-2010 12:20 AM

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

pinga123 06-23-2010 06:20 AM

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


colucix 06-23-2010 06:56 AM

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.


All times are GMT -5. The time now is 03:42 AM.