comparing a field with whole line of another file

Hello;
i have 2 file that each of them contain many records such as below:
file1

2006/01/01 12:36:49.3    37.902 48.482 0.0    1136119009
2006/01/01 13:16:22.9    34.933 46.292 2.0    1136121382
2006/01/01 13:54:33.2    37.762 48.163 18.8    1136123673
2006/01/01 14:32:9.1    37.739 48.330 23.2    1136125929
2006/01/01 21:14:32.4    36.986 46.063 18.0    1136150072

file2

2006/01/01 13:16:05.36 34.6370 46.8597 10.0    1136121379
2006/01/06 06:59:02.10 39.4099 44.6065 10.0    1136530742
2006/01/06 13:49:52.70 38.5481 40.7437 6.0      1136555392
2006/01/13 04:57:18.04 38.2197 47.8743 10.1    1137128238
2006/01/16 09:18:05.17 34.7801 45.0857 10.0    1137403085

now i want to compare each records of file1 with the whole lines of file2 and print both line of file1 and file2 which has these circumstances:
1-1st field of file1 must be equal to 1st field of file2
2-abslute difference value between 6th field of both file must be less than 5
3-abslute difference value between 3th field of both file must be less than 1
4-abslute difference value between 4th field of both file must be less than 1
i tried

NR=FNR

but i can't put circumstances in proper palace.
so for this example the output will be:
file3

 2006/01/01 13:16:22.9    34.933 46.292 2.0    1136121382  2006/01/01 13:16:05.36 34.6370 46.8597 10.0    1136121379

Try:

join -j1 file1 file2 | awk 'sqrt(($6-$11)^2)<5&&sqrt(($3-$8)^2)<1&&sqrt(($4-$9)^2)<1'

Note that it will not print column 1 from file 2...

1 Like
awk 'FNR==NR{a[$1]=$0;next}{split(a[$1],M,FS);FV=((M[6]-$6) >=0) ? (M[6]-$6) : ($6-M[6]);
SV=((M[3]-$3) >=0) ? (M[3]-$3) : ($3-M[3]); TV=((M[4]-$4) >=0) ? (M[4]-$4) : ($4-M[4]);
if(FV < 5 && SV < 1 && TV < 1){print a[$1],$0}}' file2 file1

after looking at bartus11's solution..

awk 'FNR==NR{a[$1]=$0;next}{split(a[$1],M,FS);if(sqrt(($6-M[6])^2)<5&&sqrt(($3-M[3])^2)<1&&sqrt(($4-M[4])^2)<1){
{print a[$1],$0}}}' file2 file1
1 Like