在 file2 中找到距离 file1 最近的点,shell skript
Find nearest point from file1 in file2, shell skript
我有 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)
我正在寻找这样的结果:(找到坐标最近的点ID)
Output
2036600 , xyz= -3242.42, 635.261, 1212.53, dist= 3.00
2095886 , xyz= -1111.89, 738.349, 833.635, dist= 4.36
我的算法如下所示:
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
我试图改编在这里找到的脚本,但它提供了很多行
#!/bin/bash
(($#!=2))&& { echo "Usage [=13=] 1st_file 2nd_file"; exit 1; }
awk '
BEGIN {p=fx=0; fn=""; maxd=1.1e11;}
[=13=]~"[^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]=; y1[p]=; z1[p]=;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]-; dy=y1[i]-; dz=z1[i]-; 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", , x1[mp], y1[mp], z1[mp], dist);
}
' file1.dat file2.dat
非常感谢!
$ cat tst.awk
BEGIN { OFS=", " }
NR==FNR {
points[NR] = [=10=]
next
}
{
min = 0
for (i in points) {
split(points[i],coords)
dist = ( - coords[2])^2 + \
( - coords[3])^2 + \
( - 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
我有 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)
我正在寻找这样的结果:(找到坐标最近的点ID)
Output
2036600 , xyz= -3242.42, 635.261, 1212.53, dist= 3.00
2095886 , xyz= -1111.89, 738.349, 833.635, dist= 4.36
我的算法如下所示:
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
我试图改编在这里找到的脚本,但它提供了很多行
#!/bin/bash
(($#!=2))&& { echo "Usage [=13=] 1st_file 2nd_file"; exit 1; }
awk '
BEGIN {p=fx=0; fn=""; maxd=1.1e11;}
[=13=]~"[^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]=; y1[p]=; z1[p]=;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]-; dy=y1[i]-; dz=z1[i]-; 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", , x1[mp], y1[mp], z1[mp], dist);
}
' file1.dat file2.dat
非常感谢!
$ cat tst.awk
BEGIN { OFS=", " }
NR==FNR {
points[NR] = [=10=]
next
}
{
min = 0
for (i in points) {
split(points[i],coords)
dist = ( - coords[2])^2 + \
( - coords[3])^2 + \
( - 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