KSH Convert List of Data into Multidimensional array

I have a data list like this and I wish to build a multidimensional array using korn shell (ksh)

Text file
=======
abis2273_2_D01 /db1/database/oradata/abis2273_2_d01_f01.dbf YES 5800 5616 184
abis2273_2_X01 /db2/database/oradata/abis2273_2_x01_f01.dbf YES 3600 3483 117
abis2273_D01 /db1/database/oradata/abis2273_d01_f01.dbf YES 8000 7999 1
abis2273_D01 /db1/database/oradata/abis2273_d01_f02.dbf YES 8000 7995 5
abis2273_D02 /db2/database/oradata/abis2273_d02_f01.dbf NO 9000 9000 0
abis2273_D02 /db2/database/oradata/abis2273_d02_f05.dbf YES 7500 4292 3208
abis2273_X01 /db2/database/oradata/abis2273_x01_f01.dbf YES 6400 6399 1
abis2273_X01 /db2/database/oradata/abis2273_x01_f02.dbf YES 6200 6197 3
abis2273_X02 /db1/database/oradata/abis2273_x02_f02.dbf YES 9400 9396 4
abis2273_X02 /db1/database/oradata/abis2273_x02_f03.dbf YES 8100 8099 1
abis2273_X02 /db1/database/oradata/abis2273_x02_f04.dbf YES 8500 8437 63
abis2273_X02 /db1/database/oradata/abis2273_x02_f05.dbf NO 8000 8000 0

Note that the number of column might change from 6 columns to 8 columns if the data is from another report. Therefore the script should not fix the number of column



Thanks

David
tindavidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
What do you want the multidimensional array to look like,
and what do you want to do with it?
0
TintinCommented:
I have no idea why you'd want to use a multi-dimensional array, but here you go.

#!/bin/ksh
let i=1

while read line
do
  let j=1
  for col in $line
  do
    array[$i][$j]=$col
    let j++
  done

  let i++
done <file.txt

# echo row 3 column 2
echo ${array[3][2]}

Open in new window

0
tindavidAuthor Commented:
$ ksh arr.sh
arr.sh[9]: arrary: subscript out of range  

when comment out arrary variable:    #array[$i][$j]=$col
arr.sh[13]: i++: Expression is not complete; more tokens expected.

Why
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

TintinCommented:
multi-dimensional arrays are only supported in ksh-93

What ksh version are you using?

Also, how do you want to process the data?   There's probably a much better way that using a multi-dimensional array.
0
tindavidAuthor Commented:
We are using ksh88, so cannot be tested. what about using perl, I want to further doing sorting on different column[array colum] to produce different report be display on screen.
0
ozoCommented:
#!/usr/bin/perl
$ARGV[0] ||= 'file.txt';
@array = map[split],<>;  # assuming, based on the ksh answer, that "multi" means "two"
print $array[2][1];
0
skullnobrainsCommented:
you can sort on whatever command you want using the sort command

sort -n -k 4

this will sort numerically using the 4th column

if you need sums and the likes as well, awk will probaby be the right tool
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.