2016-03-09 80 views
1

我想比較第一個文件的第二列和第二個文件的第一列,如果找到匹配,則顯示第一個文件中的所有字段和第二個文件中的所有字段。awk比較兩個CSV文件中的一列和兩個文件中的顯示字段

文件1:

"971525408953","a8:5b:78:5a:dd:dc","TRUE" 
"971558216784","ec:1f:72:24:7b:30","TRUE" 
"971506509910","e8:50:8b:d8:f3:b5","TRUE" 
"971509525934","c8:14:79:b4:bc:da","FALSE" 
"971506904830","58:48:22:83:87:7f","TRUE" 

文件2:

"fc:e9:98:1e:a2:a2",2016-03-07 23:39:29,"TRUE" 
"c8:14:79:b4:bc:da",2016-03-08 04:26:06,"TRUE" 
"78:a3:e4:87:df:19",2015-12-30 01:22:42,"TRUE" 
"18:f6:43:b1:82:47",2016-03-08 08:38:41,"TRUE" 
"58:48:22:83:87:7f",2015-12-22 01:22:42,"TRUE" 

產量預計:

"c8:14:79:b4:bc:da",2016-03-08 04:26:06,"TRUE","971509525934","c8:14:79:b4:bc:da","FALSE" 
"58:48:22:83:87:7f",2015-12-2201:22:42,"TRUE","971506904830","58:48:22:83:87:7f","TRUE" 

但是,如果我運行下面的命令,我收到沒有N [$ 2]和n [此輸出$ 3]

awk -F"," 'NR==FNR { n[$2] = $1; next } ($1 in n) {print $1,$2,$3,n[$1],n[$2],n[$3] }' file1 file2 

"c8:14:79:b4:bc:da",2016-03-0804:26:06,"TRUE","971509525934",, "58:48:22:83:87:7f",2015-12-22 01:22:42,"TRUE","971506904830",, 

任何人都可以幫助我嗎?

+0

我發現我想要的東西:) awk -F「,」-v OFS =「,」'NR == FNR {n [$ 2] = $ 1 $ 2 $ 3;下一個}($ 1 in n){print $ 1,$ 2,$ 3,n [$ 1]}'file1 file2 – skumble

回答

0

awk -F「,」-v OFS =「,」'NR == FNR {n [$ 2] = $ 1 $ 2 $ 3;下一個}(在正$ 1){打印$ 1,$ 2,$ 3,N [$ 1]}」的file1 file2的

輸出:

「C8:14:79:B4:BC:DA」,2016-03 -08 04:26:06,「TRUE」,「971509525934」「c8:14:79:b4:bc:da」「TRUE」 「58:48:22:83:87:7f」,2015-12- 22 01:22:42,「TRUE」,「971506904830」「58:48:22:83:87:7f」「TRUE」