Использование awk
:
#!/usr/bin/awk -f
# Set the Field Separator to "," and Output FS to ","
BEGIN{
FS=","; OFS=","
}
# Store each line of file1 to an array a, indexed at $1
NR==FNR{
a[$1]=$0;next
}
# Check if the column 1 of file2 is present in that array. Print if it is.
($1 in a){
print a[$1],$2,$3,$4
}
Тест:
[jaypal:~/Temp] cat f1
id1,data1,data2,data3
id2,data1,data2,data3
id3,data1,data2,data3
id4,data1,data2,data3
[jaypal:~/Temp] cat f2
id1,data4,data5,data6
id2,data4,data5,data6
id3,data4,data5,data6
id4,data4,data5,data6
[jaypal:~/Temp] ./s.awk f1 f2
id1,data1,data2,data3,data4,data5,data6
id2,data1,data2,data3,data4,data5,data6
id3,data1,data2,data3,data4,data5,data6
id4,data1,data2,data3,data4,data5,data6
Использование join
как указано @kevin
. Вот упрощенная версия.
join -t, <(sort file1) <(sort file2)
Тест:
[jaypal:~/Temp] join -t, <(sort f1) <(sort f2)
id1,data1,data2,data3,data4,data5,data6
id2,data1,data2,data3,data4,data5,data6
id3,data1,data2,data3,data4,data5,data6
id4,data1,data2,data3,data4,data5,data6