I have multiple files where i need to create a matrix with matching values
File_1, which is primary file contains all numbers tab delimited with one row
JavaScript
x
2
1
Sample_1 23 45 46 67 78 47 98 73 87 45 97 21
2
There are multiple files where if a number matches, add 1 or else add 0 to file above
File_2
JavaScript
1
9
1
Sample_2
2
23
3
67
4
47
5
235
6
87
7
102
8
97
9
File_3
JavaScript
1
10
10
1
Sample_3
2
67
3
51
4
78
5
98
6
52
7
12
8
21
9
124
10
Output
JavaScript
1
4
1
Sample_1 23 45 46 67 78 47 98 73 87 45 97 21
2
Sample_2 1 0 0 1 0 1 0 0 1 0 1 0
3
Sample_3 0 0 0 1 1 0 1 0 0 0 0 1
4
Advertisement
Answer
awk
to the rescue!
JavaScript
1
9
1
$ awk 'function pr() {for(i=2;i<=n;i++) printf "%s%d", OFS,(h[i] in p)+0; print ""}
2
NR==1 {n=split($0,h); print; next}
3
FNR==1 {if(f) pr(); delete p; printf "%s",$0} {f=1;p[$1]}
4
END {pr()}' file1 file2 file3 | column -t
5
6
Sample_1 23 45 46 67 78 47 98 73 87 45 97 21
7
Sample_2 1 0 0 1 0 1 0 0 1 0 1 0
8
Sample_3 0 0 0 1 1 0 1 0 0 0 0 1
9