common ids

I have file1.txt

I have file2.txt

and I want to extract all the rows in file1.txt that have the same idsas file2.txt in the 3rd column in the file1.txt. so the output willl be

I have tried

 
sort ${data}/13.txt > ${data}/13
sort -k3,3 ${data}/333.txt > ${data}/333
awk 'NR==FNR{a[$0];next}$3 in a' ${data}/13.txt ${data}/333.txt > ${data}/444 
 

but it gives me

Thanks

What's the point of those sorts if the sorted results are never used?

The only thing you need is that awk statement with the relevant files in the correct order piped into a single sort with the necessary options.

Regards,
Alister

1 Like

What do you mean by correct order? So would it be just
awk 'NR==FNR{a[$0];next}$3 in a' file2.txt file3.txt?

1) While still in the first file(the total number of lines and file line-number agree) load into array A.
2) Whenever the third column matches anything in A, print the line.

$ awk 'NR==FNR { A[$1]++; next } $3 in A' file2.txt file1.txt

1 2 a
3 4 b
2 3 b
1 1 a

$
1 Like

Somthing must be wrong with the file that I have or something.
Thanks! if I used A[$0], it would do the same thing right?