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
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
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
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
Summary: Since you asked "can it be done in the Korn shell", the answer is yes. Best way is to write a simple script that accepts an argument. #!/bin/ksh exec 3< /tmp/data.file Counter=1 while read -u3 Line do...
Summary: I need to check and see in a bourne shell script if a file that I want to process is open currently and being written to by another process. Any help appreciated ...
Summary: I need a little help with a Script. I need to read a filename from a directorty based on a specific format. I will be looking for the file in a directory that will have a file added to it each month. ...