Find nearest point from file1 in file2, shell skript

百般思念 提交于 2021-01-05 08:56:23

问题


I have 2 files:

file1
-3241.42  633.261  1210.53
-1110.89  735.349  836.635
(this is the points I am looking for, with coordinates x,y,z)
file2
 2014124       -2277.576          742.75        962.5816       0       0
 2036599       -3236.882         638.748        1207.804       0       0
 2036600       -3242.417        635.2612        1212.527       0       0
 2036601       -3248.006        631.6553        1217.297       0       0
 2095885       -1141.905        737.7666        843.3465       0       0
 2095886       -1111.889        738.3486        833.6354       0       0
 2095887       -1172.227        737.4004        853.9965       0       0
 2477149       -3060.679        488.6802        1367.816       0       0
 2477150       -3068.369        489.6621        1365.769       0       0
and so on
(this is the points from my model, with ID, x, y, z, 0, 0)

I am looking for such a result: (find the point IDs with nearest coordinates)

Output
2036600 ,  xyz= -3242.42, 635.261, 1212.53, dist= 3.00
2095886 ,  xyz= -1111.89, 738.349, 833.635, dist= 4.36

My algorithm would look like this:

For each line in file1, catch x1,y1,z1
  Search in file2 the nearest point, that mean dist = sqrt((x1-x2)**2+(y1-y2)**2+(z1-z2)**2) is minimum
Display the result with pointID, xyz = x2, y2, z2, dist= dist

I tried to adapt a script found here, but it gives to much lines

#!/bin/bash
(($#!=2))&& { echo "Usage $0 1st_file 2nd_file"; exit 1; }
awk '
 BEGIN {p=fx=0; fn=""; maxd=1.1e11;}
 $0~"[^0-9. \t]" || NF!=4 && NF!=3 {next;}          # skip no data lines
 fn!=FILENAME {fx++; fn=FILENAME;}                  # fx: which file
 fx==1 { if(NF!=3){printf("Change the series of two input files\n"); exit 1;}
         x1[p]=$1; y1[p]=$2; z1[p]=$3;next;}      # save the columns of first file
 fx==2 { mv=maxd; mp=0;                             # search minimal distance
           for(i=0; i<p; i++){
              dx=x1[i]-$2; dy=y1[i]-$3; dz=z1[i]-$4; dist=sqrt(dx*dx+dy*dy+dz*dz);
              if(dd<mv){mv=dd; mp=i;}               # min value & min place
           }
           printf("%3d %6.2f %6.2f %3d\n", $1, x1[mp], y1[mp], z1[mp], dist);
       }
' file1.dat file2.dat 

Thank you very much!


回答1:


$ cat tst.awk
BEGIN { OFS=", " }
NR==FNR {
    points[NR] = $0
    next
}
{
    min = 0
    for (i in points) {
        split(points[i],coords)

        dist = ($1 - coords[2])^2 + \
               ($2 - coords[3])^2 + \
               ($3 - coords[4])^2

        if ( (i == 1) || (dist <= min) ) {
            min = dist
            point = points[i]
        }
    }
    split(point,p)
    print p[1] " ", "xyz= " p[2], p[3], p[4], "dist= " sqrt(min)
}

$ awk -f tst.awk file2 file1
2036600 , xyz= -3242.417, 635.2612, 1212.527, dist= 2.99713
2095886 , xyz= -1111.889, 738.3486, 833.6354, dist= 4.35812


来源:https://stackoverflow.com/questions/64970622/find-nearest-point-from-file1-in-file2-shell-skript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!