Reading columns from a text file and to make an array for each column

Hi,
I am not so familiar with bash scripting and would appreciate your help here.
I have a text file 'input.txt' like this:
2 3 4
5 6 7
8 9 10
I want to store each column in an array like this
a ={2 5 8}, b={3 6 9}, c={4 7 10}
so that i can access any element, e.g b[1]=6 for the later use.

Please use code tags as required by forum rules!

Unfortunately, you do not mention the shell that you are using. In bash, this should do:

$ while read X Y Z; do a[++i]=$X; b=$Y; c=$Z; done < file
$ echo ${a[@]} , ${b[@]} , ${c[@]}
2 5 8 , 3 6 9 , 4 7 10

If you need the indices start with 0, rearrange the assignments in the loop.

1 Like