【问题标题】:Compare two files with the third column in each file matching but the second fith columns do not match using awk比较两个文件,每个文件匹配的第三列,但第二个第五列不匹配使用 awk
【发布时间】:2016-06-30 14:40:35
【问题描述】:

说file1是:

a b c d f
aa bb cc dd ef
ab bc dg ef ge
ao ob dy ed co

而文件 2 是:

a b c d e 
aa bb cc dd ee
ab bc de ef ge
ao ob dy ed co

预期的输出应该是:

a b c d f 
aa bb cc dd ef

这是我尝试过的:

 awk 'NR==FNR{c[$3,$5]++;next};($3 in c[$3]) && !($5 in c[$5]) > 0' file1 file2

【问题讨论】:

  • 哎呀你是对的让我编辑它!
  • 如果您将$3,$5 作为键放入c[],则搜索单个值,即$3(或$5)将永远不会匹配。祝你好运。

标签: awk sed grep


【解决方案1】:

这样的?

$ awk 'NR==FNR{a[$3]=$0;next} 
       $3 in a{split(a[$3],r); if($5!=r[5])print}' file2 file1

a b c d f
aa bb cc dd ef

检查第 5 个字段是否不匹配。

我猜,这可以简化为,

$ awk 'NR==FNR{a[$3]=$5;next} $3 in a && a[$3]!=$5' file2 file1

【讨论】:

  • 啊,我明白了,我错误地使用了数组。谢谢你的解决方案很棒!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多