2

I have a text file live below

 {
  "data": {
    "assigned-entity-id": null,
    "assigned-entity-type": null,
    "availability-domain": null,
    "compartment-id": "ocid1.tenancy.oc1..dgfmmnusmgibmkdomuijsngimimfimfgiufmgifmdsfsdsgsmgsifmg",
    "defined-tags": {
      "Oracle-Tags": {
        "CreatedBy": "abcd@gmail.com",
        "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": "abcd@gmail.com",
        "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"
}

I wanted the output as ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdebz7twdxlfdsdssssssaaaaaaadddddd ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdezzzzzzzzzzzzzzzzzzzddddddddddddddd

I tried searching on google but cannot get any specific options.

Can any one suggest any grep, awk or any usnix command to get output

Am3Y
  • 67
  • 6
  • 2
    You say "text file", but this is quite clearly JSON? As such, tools designed for JSON would probably be best (maybe for example `jq`?). – Håkan Lindqvist Mar 20 '21 at 14:07

1 Answers1

8

The most straightforward answer is to not try and utilize grep, sed, awk, etc for this - even though they will do it, you will most assuredly end up making assumptions and having to constantly shuffle the code.

You could do it with the built-in json module in python, or a purpose-built CLI tool like jq.

jq example:

% cat ids.txt | jq '.data.id'
"ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdebz7twdxlfdsdssssssaaaaaaadddddd"
"ocid1.database.oc1.cd-south-1.amaaaaaago5rdtya6mmvwjfdpdezzzzzzzzzzzzzzzzzzzddddddddddddddd"

python example:

% 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
Ackack
  • 989
  • 5
  • 11