How can I print array elements as different columns in bash?

Hi Guys,

I have an array which has numbers including blanks as follows:

1
26
66
4.77
-0.58
88
99
 
11
12
333
 

I want to print a group of three elements as a different column in a file as follows:(including blanks where there is missing elements) for.e.g. array element #7 is a blank.

1 4.77 99 12
26 -0.58 333  
66 88 11

Thanks.

awk '{_[NR%3]=_[NR%3]?_[NR%3] FS$0:$0}END{for(i in _)print _}' infile

For anything else please post on special homework subforum.

Thanks, it's not homework. It's part of a bigger program.

danmero solution will not guarantee the o/p order use below:-

awk '{a[NR%3]=a[NR%3]" "$0}
END{for (i=1;i<=3;i++) {print o= (i!=3)? a : a[0] } }' infile.txt

or

awk '((NR%3) == 1){s=s"\t"$0 }((NR%3) == 2){b=b"\t"$0}!(NR%3)
{c=c"\t"$0}END{print s"\n"b"\n"c}' infile.txt

:):):slight_smile:

Right ;), let's see now:

awk 'END{for(i=0;++i<=X;){print (i==X)?a[0]:a}}{a[NR%X]=a[NR%X]?a[NR%X]FS$0:$0}' X=3 infile