awk记录之间的距离
嘿,我想找出文本文件中记录之间的距离。我试着用awk来做。 输入示例如下:awk记录之间的距离,awk,gawk,Awk,Gawk,嘿,我想找出文本文件中记录之间的距离。我试着用awk来做。 输入示例如下: 1 2 1 4 yes 2 3 2 2 no 1 1 1 5 yes 4 2 4 0 no 5 1 0 1 no 我想找出每个数值之间的距离。我通过减去这些值,然后对答案进行平方运算。我尝试了下面的代码,但是所有的距离都是0。任何帮助都将不胜感激 BEGIN {recs = 0; fieldnum = 5;} { recs++; for(i=1;i<=NF;i++) {data[recs,i] = $
1 2 1 4 yes
2 3 2 2 no
1 1 1 5 yes
4 2 4 0 no
5 1 0 1 no
我想找出每个数值之间的距离。我通过减去这些值,然后对答案进行平方运算。我尝试了下面的代码,但是所有的距离都是0。任何帮助都将不胜感激
BEGIN {recs = 0; fieldnum = 5;}
{
recs++;
for(i=1;i<=NF;i++) {data[recs,i] = $i;}
}
END {
for(r=1;r<=recs;r++) {
for(f=1;f<fieldnum;f++) {
##find distances
for(t=1;t<=recs;t++) {
distance[r,t]+=((data[r,f] - data[t,f])*(data[r,f] - data[t,f]));
}
}
}
for(r=1;r<=recs;r++) {
for(t=1;t<recs;t++) {
##print distances
printf("distance between %d and %d is %d \n",r,t,distance[r,t]);
}
}
}
BEGIN{recs=0;fieldnum=5;}
{
recs++;
对于(i=1;i我不知道“每个数值之间的距离”在概念上是什么意思,因此我无法帮助您使用算法,但让我们整理一下代码,看看它是什么样子:
$ cat tst.awk
{
for(i=1;i<=NF;i++) {
data[NR,i] = $i
}
}
END {
for(r=1;r<=NR;r++) {
for(f=1;f<NF;f++) {
##find distances
for(t=1;t<=NR;t++) {
delta = data[r,f] - data[t,f]
distance[r,t]+=(delta * delta)
}
}
}
for(r=1;r<=NR;r++) {
for(t=1;t<NR;t++) {
##print distances
printf "distance between %d and %d is %d\n",r,t,distance[r,t]
}
}
}
$
$ awk -f tst.awk file
distance between 1 and 1 is 0
distance between 1 and 2 is 7
distance between 1 and 3 is 2
distance between 1 and 4 is 34
distance between 2 and 1 is 7
distance between 2 and 2 is 0
distance between 2 and 3 is 15
distance between 2 and 4 is 13
distance between 3 and 1 is 2
distance between 3 and 2 is 15
distance between 3 and 3 is 0
distance between 3 and 4 is 44
distance between 4 and 1 is 34
distance between 4 and 2 is 13
distance between 4 and 3 is 44
distance between 4 and 4 is 0
distance between 5 and 1 is 27
distance between 5 and 2 is 18
distance between 5 and 3 is 33
distance between 5 and 4 is 19
$cat tst.awk
{
对于(i=1;i我不知道“每个数值之间的距离”在概念上是什么意思,因此我无法帮助您使用算法,但让我们整理一下代码,看看它是什么样子:
$ cat tst.awk
{
for(i=1;i<=NF;i++) {
data[NR,i] = $i
}
}
END {
for(r=1;r<=NR;r++) {
for(f=1;f<NF;f++) {
##find distances
for(t=1;t<=NR;t++) {
delta = data[r,f] - data[t,f]
distance[r,t]+=(delta * delta)
}
}
}
for(r=1;r<=NR;r++) {
for(t=1;t<NR;t++) {
##print distances
printf "distance between %d and %d is %d\n",r,t,distance[r,t]
}
}
}
$
$ awk -f tst.awk file
distance between 1 and 1 is 0
distance between 1 and 2 is 7
distance between 1 and 3 is 2
distance between 1 and 4 is 34
distance between 2 and 1 is 7
distance between 2 and 2 is 0
distance between 2 and 3 is 15
distance between 2 and 4 is 13
distance between 3 and 1 is 2
distance between 3 and 2 is 15
distance between 3 and 3 is 0
distance between 3 and 4 is 44
distance between 4 and 1 is 34
distance between 4 and 2 is 13
distance between 4 and 3 is 44
distance between 4 and 4 is 0
distance between 5 and 1 is 27
distance between 5 and 2 is 18
distance between 5 and 3 is 33
distance between 5 and 4 is 19
$cat tst.awk
{
对于(i=1;i),请包含一些示例输出并定义距离。请包含一些示例输出并定义距离。