Comparing columns in a file

I have two files. One a small one and another one is big. The smaller one look like this:

Filename: 1.tmp

3453 0
326543 1
2321 0
3212 1

The big file looks like this:
Filename 1.res

0.3232 2321
9.2922 123
0.983 3212
8.373 326543
0.9 3453
1.098 3432

I want to extract those lines from 1.res which have common numbers in the first column of 1.tmp

My output file should be like:

0.9 3453
8.373 326543
0.3232 2321
0.983 3212

I have tried several things but none could give me the desired output.
Attempt 1:

grep -f 1.tmp 1.res

The problem with this approach is that it retrieves those values which are there in the first column in 1.res

Attempt 2:

comm 1.tmp 1.res

Attempt 3:

more 1.tmp | awk '
     NR == FNR { a[$1]=$2 }
     NR != FNR { print $2, a[$1] }
  ' 1.res

I know this code has some problems. I am using Linux with BASH.

Hi shoaibjameel123,

One way using 'awk':

$ awk 'FNR == NR { tmp[$1] = 1; next } FNR < NR { if ( tmp[$2] ) { print } }' 1.tmp 1.res 
0.3232 2321
0.983 3212
8.373 326543
0.9 3453

Regards,
Birei

1 Like

join -1 1 -2 2 <(sort -n -k1 1.tmp) <(sort -n -k2 1.res)

1 Like