Home > Software engineering >  How to use sed command to replace value in json file
How to use sed command to replace value in json file

Time:10-25

My json file looks like this:

   "parameters": {
    "$connections": {
        "value": {
            "azureblob": {
                "connectionId": "/subscriptions/2b06d50xxxxxedd021/resourceGroups/Reource1005/providers/Microsoft.Web/connections/azureblob",
                "connectionName": "azureblob",
                "connectionProperties": {
                    "authentication": {
                        "type": "ManagedServiceIdentity"
                    }
                },
                "id": "/subscriptions/2b06d502-3axxxxxxedd021/providers/Microsoft.Web/locations/eastasia/managedApis/azureblob"
            },
            "office365": {
                "connectionId": "/subscriptions/2b06d502xxxxxc8-5a8939edd021/resourceGroups/Reource1005/providers/Microsoft.Web/connections/office365",
                "connectionName": "office365",
                "id": "/subscriptions/2b06d50xxxxxx939edd021/providers/Microsoft.Web/locations/eastasia/managedApis/office365"
            }
        }
    }
}

}

I want to use sed command to replace the string in connectionId, currently my script is as follows:

script: 'sed -e ''/connectionId/c\ \"connectionId\" : \"/subscriptions/2b06d50xxxxb-92c8-5a8939edd021/resourceGroups/Reourcetest/providers/Microsoft.Web/connections/azureblob\",'' "$(System.DefaultWorkingDirectory)/function-app-actions/templates/copycode.json"'

This script can replace the strings in both connectionIds in the json file with "Resourcetest", that's what I want to make the strings in the second connectionId replace with other values, how can I do that?

I'm new to sed commands, any insight is appreciated。

Edit:

I just want to replace "Resource1005" in both connectionId strings in the json file with "Resourcetest", but I need other content in the connectionIds string to keep the previous value

So my expected output should look like this:

"connectionId": "/subscriptions/2b06d502-3axxxx8939edd021/resourceGroups/Reourcetest/providers/Microsoft.Web/connections/azureblob"


"connectionId": "/subscriptions/2b06d502-3axxxx8939edd021/resourceGroups/Reourcetest/providers/Microsoft.Web/connections/office365"

If I use the script I mentioned above, it does replace the two Resource1005s, but the other values in the string are also replaced with the same (I just want to replace the Resource1005 value)

CodePudding user response:

1st solution: With your shown samples and attempts, please try following GNU awk code. This will print only edited lines(as per shown samples) in output with substituting Resource1005 with Resourcetest in values.

awk -v RS='[[:space:]] "connectionId": "[^"]*' '
RT{
  sub(/\n [[:space:]] /,"",RT)
  sub(/\/Resource1005\//,"/Resourcetest/",RT)
  print RT
}
'  Input_file

2nd solution: With sed you can try following sed code.

sed -nE 's/(^[[:space:]] "connectionId": ".*)\/Resource1005\/(.*)/\1\/Resourcetest\/\2/p' Input_file

CodePudding user response:

Common practice is to create template files and change them with sed or something else. Like this for example:

cat template.json
...
            "office365": {
                "connectionId": "__CONNECTIONID__",
                "connectionName": "office365",
                "id": "__ID__"
            }
...

sed 's|__CONNECTIONID__|/some/path|; s|__ID__|/some/other/path|' template.json > new.json
  • Related