1

作品!!

见下文...

更新了我的脚本...

问题是它只输出外部循环中的第一行。一些简单的事情是错误的,我没有看到。

该脚本从两个文件中读取并进行计算。这是文件...

$  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

如果我在内循环上使用中断,它会正确循环,但中断不会让它正常工作,因为它每次都从匹配文件中获取第一行。我想也许这些文件需要交换。匹配文件的内循环和数据的外循环。

或者一些关于break的条件语句。

#! /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
4

1 回答 1

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" )

   }
}
于 2012-07-09T05:44:29.910 回答