neu*_*ert 11 linux bash json amazon-web-services ubuntu-20.04
我正在使用AWS CLI 客户端获取快照的状态,但输出为 JSON 格式。例如。
{
"DBClusterSnapshots": [
{
"AvailabilityZones": [
"us-east-2a",
"us-east-2b",
"us-east-2c"
],
"DBClusterSnapshotIdentifier": "...",
"DBClusterIdentifier": "...",
"SnapshotCreateTime": "2021-12-23T05:59:41.658000+00:00",
"Engine": "aurora",
"AllocatedStorage": 517,
"Status": "copying",
"Port": 0,
"ClusterCreateTime": "2020-01-17T18:59:19.045000+00:00",
"MasterUsername": "...",
"EngineVersion": "5.6.mysql_aurora.1.22.1",
"LicenseModel": "aurora",
"SnapshotType": "manual",
"PercentProgress": 0,
"StorageEncrypted": true,
"KmsKeyId": "...",
"DBClusterSnapshotArn": "...",
"SourceDBClusterSnapshotArn": "...",
"IAMDatabaseAuthenticationEnabled": false,
"TagList": []
}
]
}
Run Code Online (Sandbox Code Playgroud)
grep
我可以使用和sed
( )的组合| grep Status | sed 's/.*"Status": "//' | sed 's/",//'
来隔离“复制”的状态,但我想知道是否有更简单的方法来解析 bash 中的 JSON。例如。var['DBClusterSnapshots'][0]['Status']
use*_*686 30
是的,有几种不同的工具具有完整的 JSON 解析器和某种形式的查询语言(类似于具有 XPath 的 XML)。
jq -r .DBClusterSnapshots[0].Status
jshon -e DBClusterSnapshots -e 0 -e Status -u
但也没有什么能真正阻止您用一种具有内置 JSON 解析器并输出所需数据的语言编写单行脚本:
python -c "import sys, json; data = json.load(sys.stdin); print(data['DBClusterSnapshots'][0]['Status'])"
perl -MJSON -E '$/=undef; $data=decode_json(<>); say $data->{DBClusterSnapshots}->[0]->{Status};'
grn*_*nch 22
AWS CLI 工具具有一个内置--query
参数,该参数接受JMESPath 表达式来选择 JSON 输出的子集。
你的例子看起来像这样:
aws rds describe-db-cluster-snapshots --query "DBClusterSnapshots[0].Status"
Run Code Online (Sandbox Code Playgroud)
上述命令可能会生成类似的带引号的输出"copying"
(包含引号),因为 AWS CLI 工具默认生成 JSON 文字。
如果您只需要纯文本copying
(不带引号),请添加--output text
到上面的命令行。
归档时间: |
|
查看次数: |
2294 次 |
最近记录: |