使用shell awk获取json的某个key值

Json字符串:

curl -s http://ip-api.com/json/
{"status":"success","country":"Hong Kong","countryCode":"HK","region":"HCW","regionName":"Central and Western District","city":"Hong Kong","zip":"","lat":22.3193,"lon":114.1693,"timezone":"Asia/Hong_Kong","isp":"Shenzhen Tencent Computer Systems Company Limited","org":"Tencent Cloud Computing","as":"AS132203 Tencent Building, Kejizhongyi Avenue","query":"43.129.208.183"}%

使用,:分隔字符串,用awk遍历,并用if找到需要的key,它的下一个即是需要的key值:

curl -s http://ip-api.com/json/ | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~"status"){print $(i+1)} }}' | tr -d '"' | sed -n 1p
success
posted @ 2022-04-25 17:34  船长博客  阅读(2087)  评论(0编辑  收藏  举报
永远相信美好的事情即将发生!