bash中的反向地理编码
我有一个gps装置,可以提取经度和纬度,并作为谷歌地图链接输出bash中的反向地理编码,bash,google-maps,google-maps-api-3,scripting,Bash,Google Maps,Google Maps Api 3,Scripting,我有一个gps装置,可以提取经度和纬度,并作为谷歌地图链接输出 http://maps.googleapis.com/maps/api/geocode/xml?latlng=51.601154,-0.404765&sensor=false 从这里,我想通过curl调用它,并在第20行显示“short name” "short_name" : "Northwood", 所以我只想和你在一起 Northwood 大概是 curl -s http://maps.googleapis.co
http://maps.googleapis.com/maps/api/geocode/xml?latlng=51.601154,-0.404765&sensor=false
从这里,我想通过curl调用它,并在第20行显示“short name”
"short_name" : "Northwood",
所以我只想和你在一起
Northwood
大概是
curl -s http://maps.googleapis.com/maps/api/geocode/xml?latlng=latlng=51.601154,-0.404765&sensor=false sed sort_name
嗯,这有点快又脏:
curl -s "http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false" | grep -B 1 "route" | awk -F'"' '/short_name/ {print $4}'
Bedford Avenue
它在包含“route”的行之前查找该行,然后查找单词“short_name”,然后使用“作为字段分隔符”打印检测到的第四个字段。实际上,您应该使用JSON解析器
注:
curl -s "http://maps.googleapis.com/maps/api/geocode/xml?latlng=51.601154,-0.404765&sensor=false" | grep -B1 locality | grep short_name| head -1|sed -e 's/<\/.*//' -e 's/.*>//'
curl-s”http://maps.googleapis.com/maps/api/geocode/xml?latlng=51.601154,-0.404765&sensor=false“| grep-B1 locality | grep short|u name | head-1 | sed-e's//”
“grep-B1”查找包含“locality”的行之前的行。“grep short_name”然后获取该区域的短名称。“head-1”丢弃除第一个区域外的所有区域。sedstuff删除XML分隔符。这不是文本,而是结构化JSON。您不需要第12行冒号后面的值,而是需要结果中类型为“route”的address_组件中的short name的值 您可以使用
jsawk
或python
来实现这一点,但是使用xmlstarlet
更容易从XML输出获得它,它比python更轻,比jsawk更可用。安装xmlstarlet
并尝试:
curl -s 'http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false' \
| xmlstarlet sel -t -v '/GeocodeResponse/result/address_component[type="route"]/short_name'
这比试图将JSON解析为纯文本要健壮得多。假设您总是喜欢第12行的
短\u名称,那么以下内容似乎是可行的:
curl -s 'http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false' | sed -n -e '12s/^.*: "\([a-zA-Z ]*\)",/\1/p'
或者,如果您使用xml
api和want在第20行捕获short_name
:
curl -s 'http://maps.googleapis.com/maps/api/geocode/xml?latlng=51.601154,-0.404765&sensor=false' | sed -n -e '19s/<short_name>\([a-zA-Z ]*\)<\/short_name>/\1/p'
curl-s'http://maps.googleapis.com/maps/api/geocode/xml?latlng=51.601154,-0.404765&sensor=false'| sed-n-e'19s/\([a-zA-Z]*\)/\1/p'
有问题吗?你试过了吗?你有什么问题吗?如果是这样的话,它们是什么?感谢美国和英国的输出似乎有所不同。我已经更新了我的原始帖子,你能告诉我如何调整你的帖子吗。