Awk Comparsion在多个文件中

时间:2018-02-07 17:37:33

标签: 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中第一个文件中不存在的行{ {1}}也是。

结果输出应打印在file3中:

2,mango,man  
44,orange,oran        

芒果来自file1(第2栏),男子来自file2(第2栏)

2 个答案:

答案 0 :(得分:3)

关注awk可能对您有帮助。

awk -F, 'FNR==NR{a[$1]=$0;b[$2];next} ($1 in a) && !($2 in b){print a[$1],$2}' OFS=,   Input_file1  Input_file2

如果您想将输出输入名为file3的文件,请在上面的命令中添加> file3

答案 1 :(得分:0)

略有不同的awk:

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

说明:

dbus-send --system --dest=$BUS_NAME --print-reply $OBJECT_PATH \
org.freedesktop.DBus.Properties.Get string:com.pgaur.GDBUS string:Status
相关问题