awk比较两个CSV文件中的一列并显示两个文件中的字段

时间:2016-03-09 06:05:21

标签: csv awk

我想将第一个文件的第二列与第二个文件的第一列进行比较,如果匹配,则显示第一个文件中的所有字段和第二个文件中的所有字段。

文件1:

app.all('/api/*', function(req,res) {
   request(function(err, response){
     if(err){
       res.jsonp({'status':200,'message':'ALL OK'});
     } else {
       res.jsonp({'status':201,'message':'Something went wrong'});
     }
   });
})

file2的:

"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"

预期输出:

"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"

但如果我运行以下命令,我得到的输出没有n [$ 2]和n [$ 3]

"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"

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:0)

awk -F“,” - v OFS =“,”'NR == FNR {n [$ 2] = $ 1 $ 2 $ 3;下一个}($ 1 in n){print $ 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”