To improve "understandability" I offer another solution which uses one-dimensional arrays. This version simplifies the solution by piping two
awks. The first one transposes the matrix; the second one generates the desired result.
With this InFile ...
Code:
A1 A2 A3 A4
B1 B2 B3 B4 B5
C1 C2 C3
A1 B2 C3 A4
... these piped
awks ...
Code:
awk '{for (j=1;j<=NF;j++) a[j]=a[j]" "$j}
END {j=1; while (j in a) {print a[j]; j++}}' $InFile \
|awk '{{for (j=1;j<=NF;j++) {a[$j]++}; n=asorti(a,b);
for (j=1;j<=n;j++) print b[j],":",a[b[j]]}
print "==============="; delete a}' >$OutFile
... produced this OutFile ...
Code:
A1 : 2
B1 : 1
C1 : 1
===============
A2 : 1
B2 : 2
C2 : 1
===============
A3 : 1
B3 : 1
C3 : 2
===============
A4 : 2
B4 : 1
===============
B5 : 1
===============
Daniel B. Martin