Computing.Net > Forums > Unix > File opening and reading in KSH

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.

File opening and reading in KSH

Reply to Message Icon

Name: abby
Date: August 20, 2002 at 19:39:26 Pacific
Comment:

Hi All,
I'm trying to open a file and read its data. The file is "a.txt" which contains:
foo
bar
asdf
gfdh

I want to open this file and read the variables in an array "${values}".
Any suggestions how it can be done in KSH.
Thanks,
abby



Sponsored Link
Ads by Google

Response Number 1
Name: Nevyn
Date: August 20, 2002 at 20:36:18 Pacific
Reply:

The builtin shell command read is what you want. For the array, you will want to use the -A option.

For example: read -A vname < a.txt
This will give you an indexed array in $vname.

Complete details are in the "Built-in commands" section of the ksh(1) man page.


0

Response Number 2
Name: Wade
Date: August 20, 2002 at 22:18:17 Pacific
Reply:

read is indeed the command you need, but for the file you describe, you will need to use a loop to read each line, since read stop at the eol cahracter.


0

Response Number 3
Name: Ned
Date: August 21, 2002 at 08:37:09 Pacific
Reply:

One of the possible way is:

#!/bin/ksh

for values in `cat a.txt`
do
echo ${values}
done


0

Response Number 4
Name: Bill
Date: August 22, 2002 at 08:09:50 Pacific
Reply:

Under Korn, you can use the array properties of the "set" command:

set -A values $(cat a.txt)

OR (faster):

set -A values $(this will create a "0" based indexed array, accessible by:

echo ${values[2]}

etc


0

Response Number 5
Name: Bill
Date: August 22, 2002 at 08:11:41 Pacific
Reply:

Sorry, that last example should read:

set -A values $(


0

Related Posts

See More



Response Number 6
Name: Bill
Date: August 22, 2002 at 08:14:13 Pacific
Reply:

set -A values $(\


0

Response Number 7
Name: dar
Date: August 26, 2002 at 12:13:48 Pacific
Reply:

test


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: File opening and reading in KSH

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html

Test file Open in Bourne Shell scrp www.computing.net/answers/unix/test-file-open-in-bourne-shell-scrp/4752.html

Read File name and pass to SQLoader www.computing.net/answers/unix/read-file-name-and-pass-to-sqloader/5543.html