使用 awk 计算欧氏距离未给出预期输出

Calculation of euclidean distances with awk not giving the expected output

作为距离计算的输入,我有一个包含坐标的文件 (inp.txt):

9.911 -2.781 30.097 7.768 -4.335 29.094
11.811 -1.900 30.082 13.294 -1.993 27.440
6.947 -0.742 31.786 6.390 -2.976 30.089
8.960 -2.090 32.359 11.764 1.604 25.408
9.156 -3.314 29.815 11.764 1.604 25.408

前三列是一个点(比方说 A)的坐标,后三列是第二个点(比方说 B)的坐标。我想计算 A 和 B 之间的距离以及以下行中以相同方式排列的所有其他点。然后,在这种情况下,我希望输出 5 个距离值。

我为此使用的 awk 代码:

awk '{x1=;y1=;z1=} {x2=;y2=;z2=} END{print sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)}' inp.txt >> out.txt

输入时出现错误:

 awk: fatal: cannot open file `{x1=;y1=;z1=} {x2=;y2=;z2=} END{print sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)}' for reading (No such file or directory)

我试图弄清楚为什么会出现此错误但没有成功。 感谢您的帮助。

输入文件在你认为的地方吗?您的脚本在当前目录中使用 inp.txt 对我有用。如果我故意引用错误的文件名,则会出现与您类似的错误:

t2$ awk '{x1=;y1=;z1=} {x2=;y2=;z2=} {print sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)}' inpBAD.txt
awk: fatal: cannot open file `inpBAD.txt' for reading (No such file or directory)

此外,如果您希望每一行都有一个值,而不仅仅是最后一行,您可能不希望那里有 END 条件...

t2$ awk '{x1=;y1=;z1=} {x2=;y2=;z2=} END{print sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)}' inp.txt
7.1

t2$ awk '{x1=;y1=;z1=} {x2=;y2=;z2=} {print sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)}' inp.txt
2.83079
3.03119
2.86021
8.3561
7.1

你做了 awk -f '{x1=;...}'awk 'foo' '{x1=;...}' 而不是 awk '{x1=;...}' 所以 awk 试图打开脚本,就好像它是文件名一样。鉴于该特定错误消息,后者更有可能。看:

$ awk -f '{x1=;y1=;z1=} {x2=;y2=;z2=} END{print sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)}' inp.txt >> out.txt
awk: fatal: cannot open source file `{x1=;y1=;z1=} {x2=;y2=;z2=} END{print sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)}' for reading: No such file or directory

$ awk '7' '{x1=;y1=;z1=} {x2=;y2=;z2=} END{print sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)}' inp.txt >> out.txt
awk: fatal: cannot open file `{x1=;y1=;z1=} {x2=;y2=;z2=} END{print sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)}' for reading: No such file or directory