简体   繁体   English

从文件 2 中的文件 1 中找到最近的点,shell 脚本

[英]Find nearest point from file1 in file2, shell skript

I have 2 files:我有2个文件:

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)我正在寻找这样的结果:(找到具有最近坐标的点 ID)

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!非常感谢!

$ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM