我想从下面的命令中获取特定信息
[root@test opc]# /bin/oci compute instance list-vnics --instance-id ocid1.instance.oc1.ap-mumbai-1.anrg6ljrkfvuezyc6w4ytv75l55nscz7gg7rwzg55twubrza2uz4j7kzu66a
输出如下
{
"data": [
{
"availability-domain": "ucJX:AP-MUMBAI-1-AD-1",
"compartment-id": "ocid1.tenancy.oc1..aaaaaaaaqtzldjhvlvbaoheda67fuwkaldini7h6txiglp7yrx2nc4pm3nka",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "oracleidentitycloudservice/[email protected]",
"CreatedOn": "2022-08-23T12:08:39.326Z"
}
},
"display-name": "xx",
"freeform-tags": {},
"hostname-label": null,
"id": "ocid1.vnic.oc1.ap-mumbai-1.abrg6ljrdvonadtpflqh2zqd5f4i6nuhppq2pzdvuvr75w4mz6mvy7hdhuia",
"is-primary": false,
"lifecycle-state": "AVAILABLE",
"mac-address": "02:00:17:02:F1:53",
"nsg-ids": [],
"private-ip": "100.10.10.227",
"public-ip": null,
"skip-source-dest-check": false,
"subnet-id": "ocid1.subnet.oc1.ap-mumbai-1.aaaaaaaaa2s7zfva3efszqdlacsel7pg2doxcc5yhqt4xt25jrzah5e6ntbq",
"time-created": "2022-08-23T12:08:41.807000+00:00",
"vlan-id": null
},
{
"availability-domain": "ucJX:AP-MUMBAI-1-AD-1",
"compartment-id": "ocid1.tenancy.oc1..aaaaaaaaqtzldjhvlvbaoheda67fuwkaldini7h6txiglp7yrx2nc4pm3nka",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "oracleidentitycloudservice/[email protected]",
"CreatedOn": "2022-08-21T16:40:37.685Z"
}
},
"display-name": "test",
"freeform-tags": {},
"hostname-label": "test",
"id": "ocid1.vnic.oc1.ap-mumbai-1.abrg6ljriaxzes5wtacij7lwhcihv72sva3s5od5ngo6i4v6kv4d6zamypza",
"is-primary": true,
"lifecycle-state": "AVAILABLE",
"mac-address": "02:00:17:01:DE:74",
"nsg-ids": [
"ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaadfnpmigo5wnx6ybvjwqziabtzqtgabhsnkbsnv7qjp3euk5jpwva"
],
"private-ip": "100.10.10.214",
"public-ip": "140.238.162.238",
"skip-source-dest-check": false,
"subnet-id": "ocid1.subnet.oc1.ap-mumbai-1.aaaaaaaaa2s7zfva3efszqdlacsel7pg2doxcc5yhqt4xt25jrzah5e6ntbq",
"time-created": "2022-08-21T16:40:41.532000+00:00",
"vlan-id": null
},
{
"availability-domain": "ucJX:AP-MUMBAI-1-AD-1",
"compartment-id": "ocid1.tenancy.oc1..aaaaaaaaqtzldjhvlvbaoheda67fuwkaldini7h6txiglp7yrx2nc4pm3nka",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "oracleidentitycloudservice/[email protected]",
"CreatedOn": "2022-08-23T12:19:59.985Z"
}
},
"display-name": "yy",
"freeform-tags": {},
"hostname-label": null,
"id": "ocid1.vnic.oc1.ap-mumbai-1.abrg6ljrllmihlbt4rlhsag5jr6hewpofg6fzodmkbqnsdjqdmccyvcetpaa",
"is-primary": false,
"lifecycle-state": "AVAILABLE",
"mac-address": "02:00:17:01:B1:78",
"nsg-ids": [],
"private-ip": "100.10.10.43",
"public-ip": null,
"skip-source-dest-check": false,
"subnet-id": "ocid1.subnet.oc1.ap-mumbai-1.aaaaaaaaa2s7zfva3efszqdlacsel7pg2doxcc5yhqt4xt25jrzah5e6ntbq",
"time-created": "2022-08-23T12:20:03.295000+00:00",
"vlan-id": null
}
]
}
我想要该部分的特定 id"is-primary": true,
值为
id
private-ip
在上述情况下,输出应为
100.10.10.214
ocid1.vnic.oc1.ap-mumbai-1.abrg6ljriaxzes5wtacij7lwhcihv72sva3s5od5ngo6i4v6kv4d6zamypza
在这种情况下有人可以帮助我吗?谢谢 :)
答案1
jq
是可用于从 JSON 对象中提取数据的多种工具之一。
- json 输出:
$ jq '.data[] | select(."is-primary") | [.id, ."private-ip"]' file.json
[
"ocid1.vnic.oc1.ap-mumbai-1.abrg6ljriaxzes5wtacij7lwhcihv72sva3s5od5ngo6i4v6kv4d6zamypza",
"100.10.10.214"
]
- .csv 输出:
$ jq -r '.data[] | select(."is-primary") | [.id, ."private-ip"] | @csv' file.json
"ocid1.vnic.oc1.ap-mumbai-1.abrg6ljriaxzes5wtacij7lwhcihv72sva3s5od5ngo6i4v6kv4d6zamypza","100.10.10.214"
- 在单独的行上:
$ jq -r '.data[] | select(."is-primary") | [.id, ."private-ip"] | join("\n")' file.json
ocid1.vnic.oc1.ap-mumbai-1.abrg6ljriaxzes5wtacij7lwhcihv72sva3s5od5ngo6i4v6kv4d6zamypza
100.10.10.214
1 从技术上讲,如果有多个对象为is-primary
true,您将得到每个对象对应一个 json 数组的串联,您可以将整个jq
表达式包装在[...]
包装起来以获取一json 输出由数组组成。