I have 2 files like this:
File A:
1,x
2,z
3,y
File 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
.
The expected will be:
1,m
3,c
Just using awk
:
$ awk -F, 'NR==FNR{a[$1];next}($1 in a)' file1 file2
3,c
1,m
Pipe to sort
for ordered output:
$ 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 <(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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.