使用awk比较两个文件中的两列 [英] Comparing two columns in two files using awk
问题描述
我正在尝试以这种方式比较两个不同的文件,比如说"file1"和"file2". 如果两个文件中的字段$ 2和$ 3相同,则打印file2的$ 0.这是一个示例:
I'm trying to compare two different files, let's say "file1" and "file2", in this way. If fields $2 and $3 are the same in both files, print $0 of file2. Here's an example:
file1
E 352 697
E 25 692
E 510 744
file2
E 335 705 1 1
E 267 792 1 2
E 365 395 1 3
E 25 692 1 4
E 566 624 1 5
E 227 358 1 6
E 516 554 1 7
E 510 744 1 8
E 234 790 1 9
E 352 697 1 10
所需的输出:
E 352 697 1 10
E 25 692 1 4
E 510 744 1 8
请注意,file1中的所有对$ 2,$ 3都包含在file2中,并且输出文件的行数与file1相同. 我知道有很多类似问题的问题,但是所有答案都没有用. 我尝试使用:
Notice that all couples $2,$3 in file1 are contained in file2 and the number of rows of the output file is the same of file1. There are a lot of questions with similar problems, I know that, but all the answers were not useful. I have tried to use:
awk 'FNR==NR {a[$2]; b[$3]; next} $2 in a && $3 in b' file1 file2 > output
它可以工作,但是在输出文件中有多余的行.你可以帮帮我吗?谢谢!
It works but in the output file there are extra rows. Could you help me? Thanks!
推荐答案
awk 'NR==FNR{a[$2,$3];next} ($2,$3) in a' file1 file2
这篇关于使用awk比较两个文件中的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!