WORKS !!
见下文......
更新我的剧本......
问题是它只输出外部循环中的第一行。简单的事情是错的,我没有看到它。
此脚本从两个文件中读取并进行计算。 这是文件......
$ awk -F',' '{print $1, $2, $3, $4}' s1.txt
2017 5155 41.46097500 -78.99436944
2017 5156 41.46157778 -78.99437500
2017 5157 41.46218611 -78.99437778
2017 5158 41.46278611 -78.99438056
2017 5159 41.46339722 -78.99438333
2017 5160 41.46399444 -78.99438611
$ awk -F',' '{print $11, $12}' data.txt
41.46098533 -78.99440559
41.46217481 -78.99440703
41.46158424 -78.99439119
41.46278446 -78.99439355
41.46338829 -78.99440672
41.46399091 -78.99439231
#! /usr/bin/awk -f
BEGIN {
while (getline < "s1.txt") {
split($0, ft, ",")
line = ft[1]
station = ft[2]
lat1 = ft[3]
lon1 = ft[4]
while (getline < "data.txt") {
split($0, ft, ",")
lat2 = ft[11]
lon2 = ft[12]
x = (lon2 - lon1) * cos((lat1 + lat2) / 2)
y = (lat2 - lat1)
d = sqrt(x * x + y * y) * 6371
print line","station","lat1","lon1","lat2","lon2","d | "sort -t, -h -k7 |head -1"
}
}
}
输出..
2017,5155,41.46097500,-78.99436944,41.46098533,-78.99440559,0.198629
这会加载s1.txt中的第一行并比较data.txt中的所有行。
我需要将s1.txt推进到下一行并循环直到我到达s1.txt的末尾。
???
我添加了一些打印命令
#! /usr/bin/awk -f
BEGIN {
while (getline < "s1.txt") {
split($0, ft, ",")
line = ft[1]
station = ft[2]
lat1 = ft[3]
lon1 = ft[4]
print "loop1"
while (getline < "data.txt") {
split($0, ft, ",")
lat2 = ft[11]
lon2 = ft[12]
x = (lon2 - lon1) * cos((lat1 + lat2) / 2)
y = (lat2 - lat1)
d = sqrt(x * x + y * y) * 6371
print "loop2"
print line","station","lat1","lon1","lat2","lon2","d | "sort -t, -h -k7 |head -1"
}
}
}
看起来它有效一次。之后它不会进入第二个循环。
$ resolv.awk
loop1
loop2
loop2
loop2
loop2
loop2
loop2
loop2
loop2
loop2
loop1
loop1
loop1
loop1
loop1
loop1
loop1
2017,5155,41.46097500,-78.99436944,41.46104466,-78.99446284,0.656815
如果我在内部循环上使用了一个中断,它会正确循环但是中断不会让它正常工作,因为它每次从匹配文件中取出第一行。我想也许这些文件需要交换。匹配文件的内循环和数据的外循环。
或休息时的一些条件声明。
#! /usr/bin/awk -f
BEGIN {
while (getline < "s1.txt") {
split($0, ft, ",")
line = ft[1]
station = ft[2]
lat1 = ft[3]
lon1 = ft[4]
print "loop1"
while (getline < "data.txt") {
split($0, ft, ",")
lat2 = ft[11]
lon2 = ft[12]
x = (lon2 - lon1) * cos((lat1 + lat2) / 2)
y = (lat2 - lat1)
d = sqrt(x * x + y * y) * 6371
print "loop2"
print line","station","lat1","lon1","lat2","lon2","d
break
}
}
}
$ resolv.awk
loop1
loop2
2017,5155,41.46097500,-78.99436944,41.46104466,-78.99446284,0.656815
loop1
loop2
2017,5156,41.46157778,-78.99437500,41.46162354,-78.9943992,0.317363
loop1
loop2
2017,5157,41.46218611,-78.99437778,41.46215576,-78.99438806,0.200558
loop1
loop2
2017,5158,41.46278611,-78.99438056,41.46281488,-78.99439355,0.195243
loop1
loop2
2017,5159,41.46339722,-78.99438333,41.46458318,-78.99436234,7.55653
loop1
loop2
2017,5160,41.46399444,-78.99438611,41.46517654,-78.99437141,7.53154
loop1
loop2
2017,5161,41.46459722,-78.99438889,41.46580453,-78.99434083,7.69578
loop1
这个有用.. 我将不得不在脚本的外部排序和删除重复项,而不是在我最初计划的内部。
#! /usr/bin/awk -f
BEGIN {
while (getline < "s2.txt") {
split($0, ft, ",")
line = ft[1]
station = ft[2]
lat1 = ft[3]
lon1 = ft[4]
while (getline < "data.txt") {
split($0, ft, ",")
lat2 = ft[11]
lon2 = ft[12]
ln2 = ft[14]
stn2 = ft[15]
x = (lon2 - lon1) * cos((lat1 + lat2) / 2)
y = (lat2 - lat1)
d = sqrt(x * x + y * y) * 6371
print line","station","lat1","lon1","ln2","stn2","lat2","lon2","d
}
# edited here
close ( "data.txt" )
}
}
2017,5155,41.46097500,-78.99436944,2017,5155,41.46104466,-78.99446284,0.656815
2017,5155,41.46097500,-78.99436944,2017,5156,41.46162354,-78.9943992,4.13473
2017,5155,41.46097500,-78.99436944,2017,5157,41.46215576,-78.99438806,7.52324
2017,5156,41.46157778,-78.99437500,2017,5155,41.46104466,-78.99446284,3.42688
2017,5156,41.46157778,-78.99437500,2017,5156,41.46162354,-78.9943992,0.317363
2017,5156,41.46157778,-78.99437500,2017,5157,41.46215576,-78.99438806,3.68293
2017,5157,41.46218611,-78.99437778,2017,5155,41.46104466,-78.99446284,7.28552
2017,5157,41.46218611,-78.99437778,2017,5156,41.46162354,-78.9943992,3.58585
2017,5157,41.46218611,-78.99437778,2017,5157,41.46215576,-78.99438806,0.200558
2017,5158,41.46278611,-78.99438056,2017,5155,41.46104466,-78.99446284,11.103
2017,5158,41.46278611,-78.99438056,2017,5156,41.46162354,-78.9943992,7.40736
2017,5158,41.46278611,-78.99438056,2017,5157,41.46215576,-78.99438806,4.01615
2017,5159,41.46339722,-78.99438333,2017,5155,41.46104466,-78.99446284,14.9938
2017,5159,41.46339722,-78.99438333,2017,5156,41.46162354,-78.9943992,11.3004
2017,5159,41.46339722,-78.99438333,2017,5157,41.46215576,-78.99438806,7.90938
resolv.awk |sort -h -k9 -t, |awk -F',' '!_[$1$2]++' | awk -F',' '!_[$7$8]++'
2017,5157,41.46218611,-78.99437778,2017,5157,41.46215576,-78.99438806,0.200558
2017,5156,41.46157778,-78.99437500,2017,5156,41.46162354,-78.9943992,0.317363
2017,5155,41.46097500,-78.99436944,2017,5155,41.46104466,-78.99446284,0.656815
答案 0 :(得分:0)
好像你错过了close
声明。
#! / usr / bin / awk -f
BEGIN {
while (getline < "s1.txt") {
split($0, ft, ",")
line = ft[1]
station = ft[2]
lat1 = ft[3]
lon1 = ft[4]
while (getline < "data.txt") {
split($0, ft, ",")
lat2 = ft[11]
lon2 = ft[12]
x = (lon2 - lon1) * cos((lat1 + lat2) / 2)
y = (lat2 - lat1)
d = sqrt(x * x + y * y) * 6371
print line","station","lat1","lon1","lat2","lon2","d | "sort -t, -h -k7 |head -1"
}
# edited here
close ( "data.txt" )
}
}