Indeed two nested
while getline loops are not the solution, because after the first loop reads the first line of file1, the second loop immediately reads all the content of file2, so that only the first matching line from file1 is processed.
I would try something like this: first let awk read all the content of file1 and store each line as index of an array; second let awk read the second file and check if $1 is an index of the array:
Code:
FNR == NR {
_[$1]++
}
FNR < NR {
if ( $1 in _ ) print $1, $0
}
This uses the difference between the internal variables FNR and NR to distinguish between the two files. Run as:
Code:
awk 'FNR == NR {_[$1]++} FNR < NR {if ( $1 in _ ) print $1, $0} file1 file2
that is by passing file1 as first argument. Hope this helps.