Re: How to update a file content from a KShell script (326 Views)
Reply
Frequent Advisor
Posts: 65
Registered: ‎12-21-1999
Message 1 of 10 (326 Views)

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
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 10 (326 Views)

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
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 10 (326 Views)

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
Posts: 1,364
Registered: ‎10-14-1997
Message 4 of 10 (326 Views)

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
Posts: 881
Registered: ‎03-08-1998
Message 5 of 10 (326 Views)

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
Posts: 881
Registered: ‎03-08-1998
Message 6 of 10 (326 Views)

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
Posts: 66
Registered: ‎07-02-1998
Message 7 of 10 (326 Views)

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
Posts: 112
Registered: ‎11-11-1998
Message 8 of 10 (326 Views)

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
Posts: 1,842
Registered: ‎07-28-1997
Message 9 of 10 (326 Views)

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.
Frequent Advisor
Posts: 65
Registered: ‎12-21-1999
Message 10 of 10 (326 Views)

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
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.