比较两个文件并建立联合

时间:2013-09-11 21:51:31

标签: join awk compare union two-columns

我在下面测试了一行来比较2个文件中的第1列并建立联合。但是,消除了file2中相同第1列的不同值。下面我附上了示例文件,获得的结果和所需的结果。你能帮助我吗?

awk -F, 'BEGIN{OFS=","}FNR==NR{a[$1]=$1","$2;next}($1 in a && $2=$2","a[$1])' file2.csv file1.csv >testout.txt

file1
John,red
John,blue
Mike,red
Mike,blue
Carl,red
Carl,blue

file2
John,V1
John,V2
Kent,V1
Kent,V2
Mike,V1
Mike,V2

obtained result
John,red,John,V2
John,blue,John,V2
Mike,red,Mike,V2
Mike,blue,Mike,V2

desired result
John,red,John,V1
John,red,John,V2
John,blue,John,V1
John,blue,John,V2
Mike,red,Kent,V1
Mike,red,Kent,V2
Mike,blue,Kent,V1
Mike,blue,Kent,V2

2 个答案:

答案 0 :(得分:1)

试试这个单行:

 awk -F, -v OFS="," 'NR==FNR{a[$0];next}{for(x in a)if(x~"^"$1FS)print $0,x}' file2 file1

试验:

kent$  awk -F, -v OFS="," 'NR==FNR{a[$0];next}{for(x in a)if(x~"^"$1FS)print $0,x}' f2 f1
John,red,John,V1
John,red,John,V2
John,blue,John,V1
John,blue,John,V2
Mike,red,Mike,V1
Mike,red,Mike,V2
Mike,blue,Mike,V1
Mike,blue,Mike,V2

答案 1 :(得分:1)

使用join可以做到这一点:

join -t, -1 1 -2 1 --nocheck-order -o 1.1 1.2 2.1 2.2 file1 file2

输出:

John,red,John,V1
John,red,John,V2
John,blue,John,V1
John,blue,John,V2
Mike,red,Mike,V1
Mike,red,Mike,V2
Mike,blue,Mike,V1
Mike,blue,Mike,V2