# Line Replacement help needed.

**URL:** <https://community.unix.com/t/line-replacement-help-needed/252899>\
**Category:** Shell Programming and Scripting\
**Created:** [November 5, 2009, 3:36am UTC](https://community.unix.com/t/line-replacement-help-needed/252899 "2009-11-05T03:36:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pinga123](https://community.unix.com/letter_avatar/pinga123/32/5_5575768a8748004e209b776fc1b2916d.png) [@pinga123](https://community.unix.com/u/pinga123)\
**Post date:** [November 5, 2009, 3:36am UTC](https://community.unix.com/t/line-replacement-help-needed/252899/1 "2009-11-05T03:36:59Z")

</div>

I have a file called vm.cfg which looks like follows

```nohighlight
[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 want to replace a line starting with name = to whatever value i need to assign .For ex.in above file name is assigned 'vm\_temp' .I want to create a script which will ask user for a value for name and then assign the same.

I have used following sed command but there is little bug in it .  
Please help me to modify it.

```nohighlight
[root@OVM-SERVER1 vm_temp]# sed "s/^name/name = newvalue/g" vm.cfg

```

Above code produces following output.

```nohighlight
[root@OVM-SERVER1 vm_temp]# sed "s/^name/name = newvalue/g" 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 = newvalue = '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

```

please help.

---

<div class="post-metadata">

**Author:** ![pogdorica](https://community.unix.com/user_avatar/community.unix.com/pogdorica/32/1693_2.png) [@pogdorica](https://community.unix.com/u/pogdorica)\
**Post date:** [November 5, 2009, 3:43am UTC](https://community.unix.com/t/line-replacement-help-needed/252899/2 "2009-11-05T03:43:51Z")

</div>

Do you want this?

```nohighlight
[root@OVM-SERVER1 vm_temp]# sed "s/^name.*/name = newvalue/g" vm.cfg

```

---

<div class="post-metadata">

**Author:** ![pinga123](https://community.unix.com/letter_avatar/pinga123/32/5_5575768a8748004e209b776fc1b2916d.png) [@pinga123](https://community.unix.com/u/pinga123)\
**Post date:** [November 5, 2009, 5:47am UTC](https://community.unix.com/t/line-replacement-help-needed/252899/3 "2009-11-05T05:47:09Z")

</div>

How would i substitute new value for 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',  
]  
I want to change the first parameter i.e  
file:/var/ovs/mount/93B14928C7A6438284753B2F2AB197BB/seed\_pool/vm\_temp/System.img  
to  
some other value.

---

<div class="post-metadata">

**Author:** ![pogdorica](https://community.unix.com/user_avatar/community.unix.com/pogdorica/32/1693_2.png) [@pogdorica](https://community.unix.com/u/pogdorica)\
**Post date:** [November 5, 2009, 6:55am UTC](https://community.unix.com/t/line-replacement-help-needed/252899/4 "2009-11-05T06:55:34Z")

</div>

Hi.

If that line is all in the same line .......

```nohighlight
newfile="fjsdkfjsl"
sed "s|'file:.*,|'file:$newfile,|"

```

---------- Post updated at 12:55 PM ---------- Previous update was at 12:33 PM ----------

Hi.

If that line is all in the same line .......

```nohighlight
newfile="fjsdkfjsl"
sed "s|'file:.*,|'file:$newfile,|"

```

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/5_5575768a8748004e209b776fc1b2916d.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [November 6, 2009, 3:50am UTC](https://community.unix.com/t/line-replacement-help-needed/252899/5 "2009-11-06T03:50:58Z")

</div>

```nohighlight
echo "Pls input the name"
read name
val=`nawk -F" = " -v name="$name" '$1==name {print $2}' yourfile`
if [$val = ""];then
        echo "no such configuration item"
else
        echo $val | sed "s/'//g"
fi

```

---

<div class="post-metadata">

**Author:** ![lnviyyapu](https://community.unix.com/letter_avatar/lnviyyapu/32/5_5575768a8748004e209b776fc1b2916d.png) [@lnviyyapu](https://community.unix.com/u/lnviyyapu)\
**Post date:** [November 6, 2009, 11:32am UTC](https://community.unix.com/t/line-replacement-help-needed/252899/6 "2009-11-06T11:32:31Z")

</div>

Hi,

Try this ,

===============================  
str=`grep name cfg.txt|awk '{print $3}'`  
echo "enter the value for "  
read repstr  
sed "s/$str/\'$repstr\'/" cfg.txt \>temp
