我下面有一个文本文件
{
"data": {
"assigned-entity-id": null,
"assigned-entity-type": null,
"availability-domain": null,
"compartment-id": "ocid1.tenancy.oc1..dgfmmnusmgibmkdomuijsngimimfimfgiufmgifmdsfsdsgsmgsifmg",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "[email protected]",
"CreatedOn": "2021-03-20T13:26:04.662Z"
}
},
"display-name": "ABC",
"freeform-tags": {},
"id": "ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdebz7twdxlfdsdssssssaaaaaaadddddd",
"ip-address": "1.1.1.1",
"lifecycle-state": "AVAILABLE",
"lifetime": "XXXXXXX",
"private-ip-id": null,
"public-ip-pool-id": null,
"scope": "REGION",
"time-created": "2021-03-20T13:26:04.748000+00:00"
},
"etag": "cdsssss"
}
{
"data": {
"assigned-entity-id": null,
"assigned-entity-type": null,
"availability-domain": null,
"compartment-id": "ocid1.tenancy.oc1..dgfmmnusmgibmkdomuijsngimimfimfgiufmgifmdsfsdsgsmgsifmg",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "[email protected]",
"CreatedOn": "2021-03-20T13:26:09.523Z"
}
},
"display-name": "ASD",
"freeform-tags": {},
"id": "ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdezzzzzzzzzzzzzzzzzzzddddddddddddddd",
"ip-address": "2.2.2.2",
"lifecycle-state": "AVAILABLE",
"lifetime": "XXXXXXX",
"private-ip-id": null,
"public-ip-pool-id": null,
"scope": "REGION",
"time-created": "2021-03-20T13:26:09.583000+00:00"
},
"etag": "dfreeeee"
}
我想要的输出为 ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdebz7twdxlfdssssssaaaaaaadddddd ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdezzzzzzzzzzzzzzzzzzdddddddddddddd
我尝试在谷歌上搜索,但无法找到任何特定的选项。
有人可以建议使用 grep、awk 或任何 usnix 命令来获取输出吗
答案1
最直接的答案是不要尝试使用 grep、sed、awk 等来实现这一点 - 即使他们会这样做,你肯定会最终做出假设并不得不不断地改组代码。
你可以使用 Python 内置的 json 模块或专门构建的 CLI 工具来完成此操作,例如杰奇。
jq示例:
% cat ids.txt | jq '.data.id'
"ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdebz7twdxlfdsdssssssaaaaaaadddddd"
"ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdezzzzzzzzzzzzzzzzzzzddddddddddddddd"
Python示例:
% cat ids.txt | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['id'])"
ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdebz7twdxlfdsdssssssaaaaaaadddddd
ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdezzzzzzzzzzzzzzzzzzzddddddddddddddd