我的 json 檔案如下所示:
"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"
}
}
}
}
}
我想用sed命令替換connectionId中的字串,目前我的腳本如下:
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"'
該腳本可以將json檔案中兩個connectionId中的字串替換為“Resourcetest”,這就是我想讓第二個connectionId中的字串替換為其他值,我該怎么做?
我是 sed 命令的新手,感謝任何見解。
編輯:
我只想將json檔案中兩個connectionId字串中的“Resource1005”替換為“Resourcetest”,但我需要connectionIds字串中的其他內容來保留之前的值
所以我的預期輸出應該是這樣的:
"connectionId": "/subscriptions/2b06d502-3axxxx8939edd021/resourceGroups/Reourcetest/providers/Microsoft.Web/connections/azureblob"
"connectionId": "/subscriptions/2b06d502-3axxxx8939edd021/resourceGroups/Reourcetest/providers/Microsoft.Web/connections/office365"
如果我使用我上面提到的腳本,它確實替換了兩個Resource1005,但是字串中的其他值也替換為相同的(我只是想替換Resource1005的值)
uj5u.com熱心網友回復:
第一種解決方案:使用您顯示的示例和嘗試,請嘗試遵循 GNUawk代碼。這將僅在輸出中列印已編輯的行(根據顯示的示例),并Resource1005用Resourcetestin 值替換。
awk -v RS='[[:space:]] "connectionId": "[^"]*' '
RT{
sub(/\n [[:space:]] /,"",RT)
sub(/\/Resource1005\//,"/Resourcetest/",RT)
print RT
}
' Input_file
第二種解決方案:sed您可以嘗試以下sed代碼。
sed -nE 's/(^[[:space:]] "connectionId": ".*)\/Resource1005\/(.*)/\1\/Resourcetest\/\2/p' Input_file
uj5u.com熱心網友回復:
常見的做法是創建模板檔案并使用sed或其他方式更改它們。像這樣的例子:
cat template.json
...
"office365": {
"connectionId": "__CONNECTIONID__",
"connectionName": "office365",
"id": "__ID__"
}
...
sed 's|__CONNECTIONID__|/some/path|; s|__ID__|/some/other/path|' template.json > new.json
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/520157.html
標籤:壳sed
