Here's some fairly tricky GNU awk. GNU awk (gawk) required for arrays-of-arrays
gawk -F, '
NR == 1 {n=1; header[n] = $1}
FNR == 1 {n++; header[n] = $2; next}
!($1 in data) {data[$1][1] = $1}
{data[$1][n] = $2}
# from https://www.gnu.org/software/gawk/manual/html_node/Join-Function.html
function join(array, start, end, sep, result, i)
{
if (sep == "")
sep = " "
else if (sep == SUBSEP) # magic value
sep = ""
result = array[start]
for (i = start + 1; i <= end; i++)
result = result sep array[i]
return result
}
END {
print join(header, 1, n, FS)
PROCINFO["sorted_in"] = "@ind_str_asc" # for sorted output
for (type in data)
print join(data[type], 1, n, FS)
}
' file{1,2,3}
Type,A,B,C
CD,2,2,
FG,,3,8
QR,,,9
RR,1,,5
I'm assuming that each file has 2 columns, so it's not completely generic.
A version that does not rely on GNU awk (tested with mawk)
mawk -F, '
NR == 1 {n=1; header[n] = $1}
FNR == 1 {n++; header[n] = $2; next}
{key[$1]; data[$1,n] = $2}
END {
for (i=1; i<=n; i++)
printf "%s%s", header[i], (i==n ? ORS : FS)
for (type in key) {
printf "%s%s", type, FS
for (i=2; i<=n; i++)
printf "%s%s", data[type,i], (i==n ? ORS : FS)
}
}
' file{1,2,3}
joinmay give only common values.joincommand is able to return all lines from both files with-a 1 -a 2.-a 1 -a 2gives meQR,9,instead of the desired output.QR,,,9,