I extremely appreciate if anyone could me help to merge multiple files (up to 8) with two common columns ($1$2). I want to get all values of $3 and replace the blank with 0. Here are the samples from 4 files
File1:
chr1 111001 234
chr2 22099 108
File2:
chr1 111001 42
chr1 430229 267
File3:
chr1 111001 92
chr5 663800 311
File4:
chr1 111001 129
chr2 22099 442
Desired output
chr1 111001 234 42 92 129
chr1 430229 0 267 0 0
chr2 22099 108 0 0 442
chr5 663800 0 0 311 0
I tried
awk '{ a[$1 OFS $2 FS] = a[$1 OFS $2 FS] ( a[$1 OFS $2 FS] == "" ? "" : OFS) $3 }END{ for (i in a){print i,"0",a[i]} }' OFS="\t" file1.txt file2.txt file3.txt file4.txt | sort -k1
output
chr1 111001 0 234 42 92 129
chr1 430229 0 267
chr2 22099 0 108 442
chr5 663800 0 311
Thank very much in advance
One more variant, could you please try following, written and teste with shown samples.
Output will be as follows.
Explanation: Adding detailed explanation for above.
EDIT: As per GREAT regex GURU @anubhava sir's comments adding solution with
ARGC
andARGV
with GNUawk
.