PowerShell 中用 sed - i 替换的字段包含特殊字符

问题描述 投票:0回答:1

我需要使用 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

两者都不适合我,请帮忙。

json powershell sed
1个回答
0
投票

这应该适合你:

sed -i "s/\[parameters('test-test_properties_typeProperties')]/{}/g" file.json

重要的是要注意,我们需要在这里转义开头

[
,以告诉 sed 程序不要将其视为括号表达式,否则就像
[aeiou]

© www.soinside.com 2019 - 2024. All rights reserved.