GNU awk
. Porém, mapeará todos os valores na memória.
merge_by_headers.awk
script:
#!/bin/awk -f
BEGIN{ PROCINFO["sorted_in"] = "@ind_str_asc" }
NR==1 || FNR==1{
for (i=1; i<=NF; i++) map[i] = $i;
c = NF; next
}
NR==FNR{
row_cnt = NR-1;
for (i=1; i<=NF; i++) a[map[i]][row_cnt] = $i;
next
}
{
row_cnt += 1;
for (i=1; i<=NF; i++) a[map[i]][row_cnt] = $i;
}
END{
delete map; h = "";
for (k in a) h = (h? h OFS:"")k;
print h;
for (i=1; i<=row_cnt; i++) {
cnt = c;
for (k in a) printf "%d%s", a[k][i], (--cnt? OFS : ORS)
}
}
Uso:
awk -f merge_by_headers.awk 1.csv 2.csv | column -t
A saída:
A B C D E
1 2 4 5 6
4 5 7 8 9
3 5 6 7 8
2 3 4 5 8
66 22 10 35 43
99 15 14 48 37
88 25 33 67 56