bash - 在file2中找到离file1最近的点,shell skript
问题描述
我有 2 个文件:
文件 1 -3241.42 633.261 1210.53 -1110.89 735.349 836.635 (这是我正在寻找的点,坐标为 x,y,z)
文件2 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 等等 (这是我模型中的点,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 $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
非常感谢你!
解决方案
$ 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
推荐阅读
- javascript - Javascript Dijkstra 算法:获取 2 个节点之间的所有可能最短路径
- regex - Hive:字符串中的第一次和最后一次出现
- sql-server - Azure 弹性数据库池是否允许跨数据库事务?
- python - 我可以在 python 中检测来自特定 USB 端口的输入吗?
- javascript - 动态发布 post_id 和用户身份验证 ID 以进行评论,例如系统,其中评论表单显示为弹出窗口以评论每个帖子
- java - 用于 Java 的 SSL 配置以使用 BUS 连接 WAS MQ
- python - 字符串中以 65 开头的 10 位数字的正则表达式
- r - 如果我在 ggplot2 中设置字体系列,R studio 仅导出部分图形
- python - 根据来自另一个一维数组的值对 python 二维数组进行排序
- node.js - 使用 Node Express 应用程序尝试发送请求时出现超时错误