Computing.Net > Forums > Unix > Unix shell script for opening a file and

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Unix shell script for opening a file and

Reply to Message Icon

Name: abby
Date: July 10, 2002 at 08:28:20 Pacific
Comment:

Hi,
Can anyone tell me how to write a Unix ksh script to open a file (path provided) and assign a value to a variable in the file and then save the file with the same name ?
The variable can be present multiple times in the file but I need to change at the first instance only.



Sponsored Link
Ads by Google

Response Number 1
Name: LANkrypt0
Date: July 10, 2002 at 08:49:48 Pacific
Reply:

Ok, what do you mean by variable in the file?

Like
a=1
b=2
c=3
a=2
b=3
c=4

etc

Something along thosel lines?


0

Response Number 2
Name: abby
Date: July 10, 2002 at 08:56:58 Pacific
Reply:

Hi Lan,
Yes, the variable in the file can be of the type :
a="/usr/home/abby/dir1"
and I need to change the value of "a" to a different value, say "/usr/home/andy/dir2"

I need to do this via a script which would open the file which contains "a" (ONLY the first instance) and then change the value to the new one and save the file again.
Thanks.


0

Response Number 3
Name: LANkrypt0
Date: July 10, 2002 at 12:16:46 Pacific
Reply:

Sorry about the delay, work got busy, fast. But here is the solution.
For your reference.
$FILE is the file where the info is stored
and
$NEWDIR is the directory you want to replace the origional with

sed -e '1s/a="\/usr\/home\/abby\/dir1"\/a=$NEWDIR/;t' -e '1,/a=\/usr\/home\/abby\/dir1/s//b=$NEWDIR/' $FILE > $FILE.temp
mv $FILE.temp $FILE

Now remember when you read the input for the newdirectory you will need to place a \ infront of the / in order for sed to change it correctly. And place " " around the directory.



0

Response Number 4
Name: Jerry Lemieux
Date: July 19, 2002 at 13:44:02 Pacific
Reply:

Why not simply:

Var=/home/myhome
exec 3/tmp/file.tmp
while read -u3 Line
do
if [[ ${Line%%=*} = "Var" ]]
then
print -u4 -r "Var=$Var"
else
print -u4 -r $Line
fi
done

exec 3&-

mv /tmp/file.tmp /tmp/file


What is happening is that you are opening 2
file descriptors. One is the file you want to read, the other is the file you are writing to. Once in the loop, you are evaluating the field to the left of the = sign. If it matches, you write the new record. Otherwise, it will write the line intact into the file. Changes all occurances of the variable Var, but you said that didn't matter. Easier the playing around with sed and have to put a \ in front of each / so sed can process them.

Jerry


0

Response Number 5
Name: Jerry Lemieux
Date: July 19, 2002 at 13:47:14 Pacific
Reply:

Previous post got messed up in the paste. This is the correct one.

Why not simply:

Var=/home/myhome
exec 3/tmp/file.tmp
while read -u3 Line
do
if [[ ${Line%%=*} = "Var" ]]
then
print -u4 -r "Var=$Var"
else
print -u4 -r $Line
fi
done

exec 3&-

mv /tmp/file.tmp /tmp/file


What is happening is that you are opening 2
file descriptors. One is the file you want to read, the other is the file you are writing to. Once in the loop, you are evaluating the field to the left of the = sign. If it matches, you write the new record. Otherwise, it will write the line intact into the file. Changes all occurances of the variable a, but you said that didn't matter. Easier the playing around with sed and have to put a \ in front of each / so sed can process them.

Jerry


0

Related Posts

See More



Response Number 6
Name: Jerry Lemieux
Date: July 19, 2002 at 13:56:36 Pacific
Reply:

I still had a problem posting this. I'll try on more time. For some reason, I can't get the :

exec 3"less than sign"/tmp/file

and the second file

exec 4"greater than sign"/tmp/file.tmp

to show up. When I submit, they get removed along with some information.

Jerry



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Unix shell script for opening a file and

Shell script to read a file and pro www.computing.net/answers/unix/shell-script-to-read-a-file-and-pro/4304.html

Shell script for message tracking www.computing.net/answers/unix/shell-script-for-message-tracking-/7825.html

shell script that reads a file www.computing.net/answers/unix/shell-script-that-reads-a-file/4967.html