WORKS !!
下記参照...
更新された私のスクリプト...
問題は、外側のループの最初の行だけを出力することです。単純なことが間違っています、私にはわかりません。
このスクリプトは2つのファイルから読み取り、計算を行います。これがファイルです...
$ 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"
}
}
}
一度は動作するようです。その後、2番目のループには入りません。
$ 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