I have a data.txt
with a matrix structure (4 X 9):
101000110
000000010
001010010
100101101
I want to count the frequencies of unique columns, the expected result is:
1001 2
0000 1
1010 1
0001 3
0010 1
1110 1
I only find "unique lines according to specific columns" using awk
on the Internet, do I need to first transpose my data to solve this problem. I wonder whether there is a more direct way to figure it out? Thank you.
although not needed, here is a tranpose and count solution with unix toolset.
This
awk
will help:Result
Explanation
Stores the info in a nine column array as a key, as we know that it’s a regular matrix we will append each value to its position
Store the number into an associative array and count the occurrences
Just show the final result.
You don't need to transpose it. Use
awk
to split on empty field separator and append each value in an array indexed by column number. In theEND
block count the frequency and print it:Perl to the rescue:
-n
reads the input line by line-l
handles the newlinesaF//
split each line by characters to the @F array