Similarly I have 20 files, but now I want to match on column1, and print the corresponding values as 20 different columns
cat output
x 1 7 ..................................value_in_file20
d 9 12.................................value_in_file20
c 6 7...................................value_in_file20
FNR==1 {argc=(argc)?argc+1:2}
{v[$1];a[argc,$1]=$2}
END {
for( i in v) {
printf i
for(j=2;j<=argc;j++) {
idx=j SUBSEP i
printf("%c%s",OFS,(idx in a)?a[idx]:OFS)
}
printf ORS
}
}
If I understood it right, it prints all common records if col1 is present in two or more files. And if there is no value even if col1 is present in only two files, it would print 0 instead.
that's correct - I just changed the code print ''empty string' if a particular value is not present in col1 of a particular file. Not sure how you wanted it: 0 or 'empty field.