"Sed" while changing a key in a file

Hi,

I am new to shell scripting.

I use Solaris10 machine.I have to write a simple shell script which replaces a part of string a file.

my file name is properties.ini, which has the following values
#application properties
location.mysql = /opt/bin # ( here location.mysql is considered as #key and /opt/bin is considered as value )
location.java = /usr/bin

Now

when i run a shell script I have to replace the value of location.mysql from /opt/bin to /opt/xyz

Currently I user the following code Which i am able to replace.

#!/bin/sh
KEY=location.mysql
VALUE=/opt/xyz
FILE=./properties.ini

sed "s|^$KEY.*|$KEY=$VALUE|g" $FILE > ./temp.bak
mv ./temp.bak $FILE

The above code is working fine, but in the code after replacing the value from location.mysql=/opt/bin to location.mysql=/opt/xyz I am moving into a file called temp.bak(temporarily) , and then moving back that file into its original name(properties.ini)

Is there any solution to replace the value in the file permanently with out moving the files.

Thanks in advance,
RaghuDeep Amilineni

If you have GNU sed, you can use the -i flag.

Else you may want to use perl. See if this works

perl -p -i -e "s|^$KEY.*|$KEY=$VALUE|g" $FILE

hi vino,

Can you please explain me what is GNU sed.

I should not user perl for this code.

GNU sed is the sed utility available as part of the GNU Project. You can get it from GNU sed - GNU Project - Free Software Foundation (FSF)