Home > OS >  Look for particular substring and then perform changes to next occuring variable using shell command
Look for particular substring and then perform changes to next occuring variable using shell command

Time:11-01

i do have a JSON file as below:-

{
  "Rules": [
    {
      "ID": "DMS-FULL-HISTORY-DATA-DEEPARCHIVE-1d",
      "Filter": {
        "Prefix": "HISTORY/DATA/"
      },
      "Status": "Enabled",
      "Transitions": [
        {
          "Days": 1,
          "StorageClass": "DEEP_ARCHIVE"
        }
      ]
    },
    {
      "ID": "FOL-FULL-HISTORY-DATA-DEEPARCHIVE-1d",
      "Filter": {
        "Prefix": "HISTORY/DATA/"
      },
      "Status": "Enabled",
      "Transitions": [
        {
          "Days": 1,
          "StorageClass": "DEEP_ARCHIVE"
        }
      ]
    },
    {
      "ID": "Hello-CDC-delete-30d",
      "Filter": {
        "Prefix": "CDC/"
      },
      "Status": "Enabled",
      "Expiration": {
        "Days": 30
      }
    }
  ]
}

I want to only lookout for the rule IDs containing '-FULL-HISTORY-DATA' and then updating its Status to "Disabled". Rest file remains unchanged. Is there a way to do edit without opening the file using single shell command or sed?

Desired output:-

{
  "Rules": [
    {
      "ID": "DMS-FULL-HISTORY-DATA-DEEPARCHIVE-1d",
      "Filter": {
        "Prefix": "HISTORY/DATA/"
      },
      "Status": "Disabled",
      "Transitions": [
        {
          "Days": 1,
          "StorageClass": "DEEP_ARCHIVE"
        }
      ]
    },
    {
      "ID": "FOL-FULL-HISTORY-DATA-DEEPARCHIVE-1d",
      "Filter": {
        "Prefix": "HISTORY/DATA/"
      },
      "Status": "Disabled",
      "Transitions": [
        {
          "Days": 1,
          "StorageClass": "DEEP_ARCHIVE"
        }
      ]
    },
    {
      "ID": "Hello-CDC-delete-30d",
      "Filter": {
        "Prefix": "CDC/"
      },
      "Status": "Enabled",
      "Expiration": {
        "Days": 30
      }
    }
  ]
}

I have tried using sed , but that is performing update to all rule ids.

CodePudding user response:

If you really want a sed command, you can use the following :

sed '/.*FULL-HISTORY-DATA.*/{N;N;N;N;s/"Status": "Enabled"/"Status": "Disabled"/}'

You can try it here.

Every time sed encounters a line that contains 'FULL-HISTORY-DATA', it consumes the next 4 lines of data and replace "Status": "Enabled" by "Status": "Disabled" in them.

This is a very fragile process as any unsignificant change in JSON formatting will make it fail, as will having two occurences of FULL-HISTORY-DATA in less than 4 lines (the second will be consumed when the first is handled and won't be properly processed by the command).

If you need something reliable and robust you will want to use a JSON parser.

CodePudding user response:

If the jq command-line JSON parser is available, you can use the following command :

jq '.Rules |= map((select(.ID | contains("FULL-HISTORY-DATA"))| .Status = "Disabled" ) // .)'

You can try it online.

  • Related