• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1347
  • Last Modified:

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


  • 2
  • 2
  • 2
  • +1
3 Solutions
What do you want the multidimensional array to look like,
and what do you want to do with it?
I have no idea why you'd want to use a multi-dimensional array, but here you go.

let i=1

while read line
  let j=1
  for col in $line
    let j++

  let i++
done <file.txt

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

Open in new window

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.

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

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.
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.
$ARGV[0] ||= 'file.txt';
@array = map[split],<>;  # assuming, based on the ksh answer, that "multi" means "two"
print $array[2][1];
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now