2
votes

I have searched high and low, but not finding the exact code for awk.

I have 2 files. File 1 (single column):

1407859648
1639172851
1427051689
1023011285
1437152683
1508869405
1790775963
1932373552

File 2 (three columns):

1790775963,1932373552,65
1639206006,1437337425,15
1265418669,1477541563,145
1053424648,1316944317,182
1184611535,1821014457,26
1003906082,1134327133,152
1376530121,1841236684,168
1316921570,1962555771,23
1396962627,1184732489,87
1194958421,1255333456,113
1538156732,1336215482,62

File 1 and 2 have an unequal number of records. I would like to print records from File 2, when both Col1 and Col2 in File2 match Col1 from File1. In this example output should be:

1790775963,1932373552,65

Thank you!

A

1

1 Answers

2
votes

Try following:

awk -F',' 'NR==FNR {arr[$1]++; next} (($1 in arr) && ($2 in arr)) {print $0}' file1 file2

Output:

1790775963,1932373552,65

EDIT

Or more concisely as suggested by sudo_O

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