AWK在shell脚本 - 如何比较和合并基于共享密钥(2公共字段)两个文件? [英] AWK in shell script - How to compare and merge TWO files based on a shared key (2 common fields)?

查看:125
本文介绍了AWK在shell脚本 - 如何比较和合并基于共享密钥(2公共字段)两个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否有人请帮忙比较两个文件,
我用下面的命令,但我想不出它成功了,

awk的-F,'NR == FNR {a [$ 1,$ 2];接下来}(($ 1,$ 2)一)temp1.dat temp2.​​dat

下面是我的需要,
首先需要两个字段比较的空两DAT文件和合并的结果预计将在文件3(第一场,第二场,temp1.dat的3场,temp2.​​dat的3场)

文件1:temp1.dat

  A,AB,100
B,BB,200
C,CC,300

文件2:temp2.​​dat

  A,AB,10
C,CC,30
D,DF,4

FILE3:输出

  A,AB,100,10
C,CC,300,30


解决方案

的awk -F,'BEGIN {OFS =,} FNR == NR {a [$ 1 $ 2] = $ 3;下一个}($ 1 $ 2在急症室;&安培; $ 3 = $ 3,一[$ 1 $ 2])文件2文件1

以下测试:

 >猫文件1
A,AB,100
B,BB,200
C,CC,300
>猫文件2
A,AB,10
C,CC,30
D,DF,4
> awk的-F,'BEGIN {OFS =,} FNR == NR {a [$ 1 $ 2] = $ 3;接下来}($ 1 $ 2&安培;&安培; $ 3 = $ 3,一[$ 1 $ 2])'文件2文件1
A,AB,100,10
C,CC,300,30
>


  • FNR == NR {a [$ 1 $ 2] = $ 3;接下来} 被所申请的第一个文件的文件2

  • 它说,直到 FNR == NR 执行code的德块。

  • 当前文件的FNR =行数

  • 两个文件的总线条
  • NR =行号。

  • 所以上面的语句关联数组索引为 $ 1 $ 2 和值 $ 3
  • 现在,($ 1 $ 2在急症室;&安培; $ 3 = $ 3,一[$ 1 $ 2])
    这个执行用于 FNR!= NR 。凡在它检查指标$ 1 $ 2存在的数组中的一个索引,然后德第二个条件正在改变file1的第三个字段设置为 $ 3 = $ 3,一[$ 1 $ 2] 所以现在 $ 1,0 包含了常见的( $ 1这在他们的第三个变化场$ 2 )线。

相似的逻辑已经为四个文件也被写入。

Could some one please help to compare two files, i have used the below command but i couldnt succeed on it,

awk -F, 'NR == FNR {a[$1,$2]; next} (($1,$2) in a )' temp1.dat temp2.dat

Here is my need, need to compare first two fields in the below two dat files and merge the result as expected in file3(first field, second field, 3 field of temp1.dat, 3 field of temp2.dat)

File1:temp1.dat

A, AB,100
B,BB,200
C,CC,300

File2:temp2.dat

A,AB,10
C,CC,30
D,DF, 4

File3 :output

A, AB,100,10
C,CC,300,30

解决方案

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

tested below:

> cat file1
A,AB,100
B,BB,200
C,CC,300
> cat file2
A,AB,10
C,CC,30
D,DF,4
> awk -F, 'BEGIN{OFS=","}FNR==NR{a[$1$2]=$3;next}($1$2 in a && $3=$3","a[$1$2])' file2 file1
A,AB,100,10
C,CC,300,30
> 

  • FNR==NR{a[$1$2]=$3;next} is applied for the first file file2
  • it says untill FNR==NR execute teh block of code.
  • FNR=line number of the current file
  • NR= line number of the total lines of two files.
  • so after the above statement an associative array with index as $1$2 and value as $3.
  • Now ($1$2 in a && $3=$3","a[$1$2]) this executes for FNR!=NR.where in it checks for index $1$2 exists as an index in the array and then teh second condition is changing the 3rd field of file1 to $3=$3","a[$1$2]so now $0 contains the common($1$2) lines which changed 3rd field in them.

similar logic has to be written for four files also.

这篇关于AWK在shell脚本 - 如何比较和合并基于共享密钥(2公共字段)两个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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