以下のコマンドから特定の情報を取得したいと思います。
[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オブジェクトからデータを抽出するために使用できるいくつかのツールの1つ。
- 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技術的に複数のオブジェクトがtrueの場合、is-primary
各オブジェクトに対して1つのjson配列を連結し、式jq
全体を[...]
ラッパーでラップすることによって取得できます。一つjson出力は配列で構成されています。