使用 jq 将字符串中的占位符替换为实际值

Replace the placeholder in the String with actual value using jq

它不起作用。如何检查字符串的一部分(而不是整个字符串)?

test.json

{
 "url": "https://<part1>.test/hai/<part1>"
}



jq --arg input "$arg" \
   'if .url == "<part1>"
     then . + {"url" : ("https://" + $input + ".test/hai/" + $input)  }
       else . end' test.json  > test123.json

在你的 test.json 中,.url 的值是 "https://<part1>.test/hai/<part1>" 所以显然你不想检查它的值是 "<part1>".

也许你想测试条件:.url | contains("<part1>") 或者 .url | endswith("<part1>") -- 问题描述不清楚。

如果您的 jq 支持正则表达式,您可以使用 test/1,例如

.url | test("//<part1>.*<part1>$")

另一种可能性是使用 gsub.