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
本文通过curl获取IP地址信息,利用awk提取JSON中的'status'和'country'字段,展示了如何用简单的命令行工具处理JSON数据并找到所需信息。

被折叠的 条评论
为什么被折叠?



