[英]Match 2 fields in different files
I have 2 files like this: 我有2个这样的文件:
File A: 档案A:
1,x
2,z
3,y
File B: 档案B:
7,b
3,c
9,t
1,m
I would like to loop through File A
(first columns) and see if there any matches in File B
(first columns) using awk
. 我想遍历
File A
(第一列),并使用awk
File B
(第一列)中是否有任何匹配项。
The expected will be: 预期将是:
1,m
3,c
Just using awk
: 只是使用
awk
:
$ awk -F, 'NR==FNR{a[$1];next}($1 in a)' file1 file2
3,c
1,m
Pipe to sort
for ordered output: 用于
sort
有序输出的管道:
$ awk -F, 'NR==FNR{a[$1];next}($1 in a)' file1 file2 | sort
1,m
3,c
Alternatively this is what join
does (requires input files to be sorted) : 另外,这就是
join
作用(需要对输入文件进行排序) :
$ join <(sort file1) <(sort file2) -j1 -t, -o"2.1,2.2"
1,m
3,c
join -t, -o 1.1,1.2 <(sort fileA) <(sort fileB)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.