在Linux上使用Shell脚本解析json [英] parse json with shell scripting at linux

查看:89
本文介绍了在Linux上使用Shell脚本解析json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用sed和awk解析json

Possible Duplicate:
Parsing json with sed and awk

我有一个如下例所示的JSON字符串,并且想在外壳脚本中使用"uptime"命令的值作为变量,该怎么办?

I have a JSON string like the example below and I want to use, for example, the value of the "uptime" command as a variable in a shell script, what should I do?

{"serverStatus":{"version":"1.6.0","uptime":527,"uptimeEstimate":526,"localTime":{"$ date":1286923624579},"globalLock":{"totalTime":526604302,"lockTime":3499842,"ratio":0.006646056605895331,"currentQueue":{"total":0,"readers":0,"writers":0}},"mem":{"bits" :64,居民":150,虚拟":76114,支持":true,映射":75950},连接":{当前":1,可用":9599},"extra_info": {"note":字段因平台而异","heap_usage_bytes":600592,"page_faults":1838},"indexCounters" ....

{ "serverStatus" : { "version" : "1.6.0", "uptime" : 527, "uptimeEstimate" : 526, "localTime" : { "$date" : 1286923624579 }, "globalLock" : { "totalTime" : 526604302, "lockTime" : 3499842, "ratio" : 0.006646056605895331, "currentQueue" : { "total" : 0, "readers" : 0, "writers" : 0 } }, "mem" : { "bits" : 64, "resident" : 150, "virtual" : 76114, "supported" : true, "mapped" : 75950 }, "connections" : { "current" : 1, "available" : 9599 }, "extra_info" : { "note" : "fields vary by platform", "heap_usage_bytes" : 600592, "page_faults" : 1838 }, "indexCounters"....

推荐答案

您可以将编程语言与json模块一起使用,或者如果需要shell + * nix工具解决方案,则可以使用awk

You can use a programming language with a json module, or if you desire a shell+*nix tool solution, you can use awk

$ awk -F"[,:]" '{for(i=1;i<=NF;i++){if($i~/uptime\042/){print $(i+1)} } }' file
 527

这篇关于在Linux上使用Shell脚本解析json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆