Awk比较多个文件 [英] Awk Comparsion in multiple files

查看:77
本文介绍了Awk比较多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件:

file1:

1,apple  
2,mango  
3,banana  
44,orange  

file2:

1,apple  
22,  
31,xyz  
2,man  
3,banana  
44,oran   
44,orange

我需要使用第1列和检查第2列来查找这两个文件之间的差异.我不想使用$0作为其打印也不会出现在file2中的第一个文件行的内容.

I need to find the differences from both the files using column 1 and checking column 2. I don't want to use $0 as its printing the lines which of 1st file which are not present in file2 too.

结果输出应在file3中打印为:

Result output should be printed in file3 as :

2,mango,man  
44,orange,oran        

芒果来自file1(第2列),而人类来自file2(第2列)

Mango is from file1 (column 2) and man is from file2 (column2)

推荐答案

稍有不同的awk:

$ awk 'BEGIN{FS=OFS=","}($1 in a) && a[$1]!=$2{print $1,a[$1],$2}{a[$1]=$2}' file1 file2
2,mango,man  
44,orange,oran  

解释:

awk 'BEGIN {
    FS=OFS=","            # set separators
}
($1 in a) && a[$1]!=$2 {  # if the id is in a and $2s differ   (may occur only after file1 
    print $1,a[$1],$2     # output                              is already hashed into a)
}
{
    a[$1]=$2              # hash to a
}' file1 file2

这篇关于Awk比较多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆