How to convert 2 columns into matrix -awk?

How can i convert two columns in to o and 1 matrix. thnks

Input
a c1
b c2
c c1
d c3
e c4

output

        c1      c2      c3      c4
a       1       0       0       0
b       0       1       0       0
c       1       0       0       0
d       0       0       1       0
e       0       0       0       1

You had a very similar thread some time ago. Any learnings from that?

However, try

awk     '       {LN[$1]; HD[$2]; MX[$1,$2]++}
         END    {               printf "%10s", ""; for (i in HD) printf "%10s", i; print "";
                 for (j in LN) {printf "%10s",j;   for (i in HD) printf "%10s", MX[j,i]+0; print ""}
                }
        ' file
                  c1        c2        c3        c4
         a         1         0         0         0
         b         0         1         0         0
         c         1         0         0         0
         d         0         0         1         0
         e         0         0         0         1
1 Like

you are right. however it was based on 3 columns and here it is just two.
I noticed that if the column names are longer for example instead of c1 c1_abajgdhd_1234x, the script is printing bit wrong because some header names were not separated by tab.

I guess that's due to the <CR> (0x0D, \r) windows line terminators that you use. Remove them beforehand.

Thanks but I tried this and didn't work

tr -d '\r' <$1 | awk     '       {LN[$1]; HD[$2]; MX[$1,$2]++}
         END    {               printf "%10s", ""; for (i in HD) printf "%10s", i; print "";
                 for (j in LN) {printf "%10s",j;   for (i in HD) printf "%10s", MX[j,i]+0; print ""}
                }
        '

Works perfectly for me. Try increasing the %10s format string.