linux - 如何使用 jq 获取数组值
问题描述
文件.json
{
"Groups":[
],
"Instances":[
{
"AmiLaunchIndex":0,
"ImageId":"ami-id",
"InstanceId":"i-123548874",
"InstanceType":"c5.4xlarge",
"KeyName":"kay",
"LaunchTime":"dateValue",
"Monitoring":{
"State":"disabled"
},
"Placement":{
"AvailabilityZone":"zone value",
"GroupName":"",
"Tenancy":"default"
},
"PrivateDnsName":"ip address",
"PrivateIpAddress":"10.245.247.21",
"ProductCodes":[
],
and more json...
数组中只有一项。我想从 json中获取InstanceId和PrivateIpAddress 。我正在使用以下命令来获取值:
file.json | jq -r .Instances[].InstanceId
file.json | jq -r .Instances[].PrivateIpAddress
但我没有得到任何回应。
我尝试了其他堆栈溢出问题中提到的各种方法,但仍然没有得到正确的响应。
我期待以下回应
i-123548874
10.245.247.21
解决方案
$ cat file.json | jq -r .Instances[].PrivateIpAddress
10.245.247.21
$ cat file.json | jq -r .Instances[].InstanceId
i-123548874
或者
$ jq -r .Instances[].PrivateIpAddress file.json
10.245.247.21
$ jq -r .Instances[].InstanceId file.json
i-123548874
推荐阅读
- r - 在 FlexDashboard 中过滤。过滤选项不是数据集的一部分
- reactjs - 当回调在父级中更改状态时,如何使用 React.memo 和 useCallback 优化 React 组件
- git - Git 在上一个差异之后添加
- angular - 角度测试因 takeWhile() 而失败
- laravel - 我从远程服务器上读到了一些难看的字母
- c# - 在 Unity Vuforia 插件中捕获 VideoBackground 的快照
- c# - LINQ to SQL:插入子实体失败,因为 SQL 中不包含 ID?
- c# - 实体框架不刷新数据
- java - 使用 ReadOnlyPDOMProvider 导入索引内容
- c - C标准中可变修改类型的switch语句约束说明