在 Azure CLI 和 REGEX 中使用变量

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

我尝试在 Azure CLI 中使用 Regex 替换 2 个分隔符之间的字符串,并将变量输入到我的 Regex 表达式中,但我所使用的语法未按预期工作。我尝试过以下脚本:

originalString = "URL":"some-url" "port":"580" "value":"test123"
newValue = "ABC123"
newString = \originalString | sed -e 's/\".*\"/\"newValue\"/'
echo newString 

预期的输出应该是 "URL":"some-url" "port":"580" "value":"ABC123" 但它在前 3 行抛出了消息“command not find”。

看来我无法像上面尝试的那样将变量插入正则表达式中。请让我知道我做错了什么。

azure azure-cli
1个回答
0
投票

预期的输出应该是 "URL":"some-url" "port":"580" "value":"ABC123" 但它在前 3 行抛出了消息“command not find”。

您可以使用以下命令来获得预期的结果:

rithwikstring='"URL":"test-url" "port":"681" "value":"rithwik1234test"'
rithwiknew="ABC123"
resultstring=$(echo $rithwikstring | sed -e "s/\"value\":\"[^\"]*\"/\"value\":\"$rithwiknew\"/")
echo "$resultstring"

Output:

enter image description here

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