Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read a tax file

Status
Not open for further replies.

alhassani54

Programmer
Aug 1, 2002
27
GB
Hi

I have a text file "abc" and I would like to read it.

the "abc" text file is
----------------------
abc
def
ghi

the script is
-------------

#! /bin/ksh
#
integer count=0
export count
exec 0<abc
until (( count==3 ))
do
read line
if [[ $count = 0 ]]; then a=$line; fi
if [[ $count = 1 ]]; then b=$line; fi
if [[ $count = 2 ]]; then c=$line; fi
((count+=1))
done
print $a
print $b
print $c
# End of script

When I run the script I get
abc
def
ghi

Is there a way to simplify the script to read the text file?

All help will be appreciated.



 
Hi:

How about reading file &quot;abc&quot; into an array?

#!/bin/ksh
count=0

while read line
do
alines[count]=$line
((count+=1))
done < abc

echo ${alines[*]}

# or

echo ${alines[0]}
echo ${alines[1]}
echo ${alines[2]}
 
Thank you &quot;olded,&quot; finally, someone else who uses arrays within korn shell. I use arrays all of the time in ksh but rarely does one see them used. It seems that most people use the ksh, bsh, etc. to sring a number of commands together in a script and call it good. I try to use the full power of the shell that is available to me.

One time I wrote a shell script using arrays and loops and when I was finished it numbered around 50 lines. A guy I work with, who thinks he is THEE scripting god, said he could do it better; a little while later he was finished and had it done in half of the lines I had. He had used a number of commands strung together (cut, grep...) and claimed that it would also be faster. When they were ran mine beat his by a few seconds. He claimed he needed to &quot;tweak&quot; it, and after his &quot;tweaking&quot; I still beat him by a couple of seconds.

Moral is... use the full power of the shell that is given to you. Book titles on shell do call it &quot;shell programming.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top