I'm trying to get the total (sum of) counts of each uniq string in every column, with output in corresponding column order.
I need this in a powerful awk command, as the varying full input is usually thousands of rows and columns.
I've tried to do this myself and haven't had any luck. I think I'm close-ish, here's where I got to with the code, though it obviously doesn't work:
awk -F ',' '{ for(N=1; N<=NF; N++) {{count[$N]++} END {for (word in count) print word, count[word]}}}'
My thinking for the above code was that I could get the desired output for a single specified column, for now lets say column 2, if i ran:
awk '{count[$2]++} END {for (word in count) print word, count[word]}'
However I need that type of output for every column. So I attempted to loop through the columns to achieve this, but it failed miserably :(
Here's some example data:
Example input:
M,M,M,M
N,N,N,N
A,M,G,L
P,P,P,P
A,N,G,L
P,N,P,L
A,A,A,A
C,C,C,C
A,M,G,C
L,L,L,L
G,G,G,G
Corresponding desired output:
M 1,M 2,M 1,M 1
N 1,N 2,N 1,N 1
A 3,A 1,A 1,A 1
P 1,P 1,P 1,P 1
L 1,L 1,L 1,L 3
G 1,G 1,G 3,G 1
C 1,C 1,C 1,C 1
To explain the output, the first column in example input has 3 A's, and all other letter only have 1 each, so the output for that column is:
M 1
N 1
A 3
P 1
L 1
G 1
C 1
I wrote this code and it would work, but ideally I would like to achieve it within the awk command:
for i in $(seq $NumberOfColumns);do
ColumnOutput=$(awk -F ',' -v x=$i '{count[$x]++} END {for (word in count) print word, count[word]}' file)
TotalOutput=$(paste <(echo "$TotalOutput") <(echo "$ColumnOutput") -d ,)
done
echo "$TotalOutput" | sed 's/^,//g'
A, N, G, L
) not part of the output?