我需要使用 Sed 来更新 Json 文件中的一些字符串。但是由于字段中存在特殊字符,导致替换没有成功:
需要替换的字符串是:
"[parameters('test-test_properties_typeProperties')]"
我想将其替换为
{}
我的测试代码如下:
sed -i "s/'"[parameters('test-test_properties_typeProperties')]"'/{}/g" $(Build.Repository.LocalPath)/ARMTemplate/ARMTemplateParametersForFactory.json
或
sed -e "s|"'['parameters'(''test-test_properties_typeProperties'')'']'"|{}|g" $(Build.Repository.LocalPath)/ARMTemplate/ARMTemplateForFactory.json
两者都不适合我,请帮忙。
这应该适合你:
sed -i "s/\[parameters('test-test_properties_typeProperties')]/{}/g" file.json
重要的是要注意,我们需要在这里转义开头
[
,以告诉 sed 程序不要将其视为括号表达式,否则就像 [aeiou]
。