Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

How to update a file content from a KShell script

Frequent Advisor

How to update a file content from a KShell script

I have a parameter file which contains severail variables declaration. I want to build a ksh script which read that file, find the proper variable I look for and change is value.
How can I do that ?
--------------
Example:
(content of param.var)
LOCAL_VAR1="ABC"
LOCAL_VAR2="TOTO"
LOCAL_VAR3=25

-> Must change LOCAL_VAR2 to "TEST" ?
Sysd. Amin. Inforef
9 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: How to update a file content from a KShell script

Hi:

Try this:

# cat master_script | sed s/LOCAL_VAR2=\"TOTO\"/LOCAL_VAR2=\"XXXX\"/g > new_script

...JRF...
Acclaimed Contributor Acclaimed Contributor

Re: How to update a file content from a KShell script

Hi:

Try this:

# cat master_script |sed /LOCAL_VAR2=\"TOTO\"/LOCAL_VAR2=\"XXXX\"/g > new_script

Note the backslashes to escape the quote marks in your master_script.

...JRF...
Honored Contributor Honored Contributor

Re: How to update a file content from a KShell script

Hi,

you could also do this:

awk -F= '{
if($1 == "LOCAL_VAR2")
print $1 "=" "\"TEST\""
else
print $1 "=" $2
}' param.var >param_tmp.var
mv param_tmp.var param.var

For multiple choices add some else if (...)
statements before the else statement.

Regards

Andrew
Honored Contributor Honored Contributor

Re: How to update a file content from a KShell script

Just to be different:

IFSSAVE="$IFS"
IFS="="
while read VAR VALUE
do
if [ "$VAR" = "{variable you want to change"} ]
then
echo "$VAR={new value}" >> tmpfile
else
echo "$VAR=$VALUE" >> tmpfile
fi
done < file
mv tmpfile file

Now, the sed really makes more sense, and sed is a great thing to become familiar with, but it is sometimes easier to modify/maintain a script if you are more familiar with exactly what it does, so I figured I would give you this alternative.

But play around with sed, too. It will be worth your time.
Honored Contributor Honored Contributor

Re: How to update a file content from a KShell script

Just to be different:

IFSSAVE="$IFS"
IFS="="
while read VAR VALUE
do
if [ "$VAR" = "{variable you want to change"} ]
then
echo "$VAR={new value}" >> tmpfile
else
echo "$VAR=$VALUE" >> tmpfile
fi
done < file
mv tmpfile file

Now, the sed really makes more sense, and sed is a great thing to become familiar with, but it is sometimes easier to modify/maintain a script if you are more familiar with exactly what it does, so I figured I would give you this alternative.

But play around with sed, too. It will be worth your time.
Frequent Advisor

Re: How to update a file content from a KShell script

Hi.

I would use

cat param.var|sed 'LOCAL_VAR2/TEST' >newparam

I hope this helps

Dave
why do i do this to myself
Trusted Contributor Trusted Contributor

Re: How to update a file content from a KShell script

Yet another way to do it, with the advantage that you don't need any temporary files.

#!/usr/bin/ksh
# Change paramfile eg. $HOME/params
#
cat - << EOF | ed -s $HOME/params
1,$ s:LOCAL_VAR2="TOTO":LOCAL_VAR2="TEST":
w
q
EOF

Could of course be made more generic, by passing name of parameter file and which parameter to change as arguments.
Keep it simple
Honored Contributor Honored Contributor

Re: How to update a file content from a KShell script

I'm not sure what you are trying to achieve here. If you want to edit the param file from within a script then you can use 'ex' (a command line version of vi). Example:-

#!/usr/bin/sh
ex param.var </LOCAL_VAR2
s/TOTO/TEST/
wq!
EOD

If you want to get the values for your variables into the script then you can just 'dot' the file then maybe reassign any variables that you want to override. For example:-

. param.var
LOCALVAR2=TEST

If you are trying to do anything else, please supply some more details.
Highlighted
Frequent Advisor

Re: How to update a file content from a KShell script

Finaly, I use the sed command and it works correctly.

Thanks for your help.
Sysd. Amin. Inforef