sed 编辑 yaml 文件,每个键都有单独的值

sed to edit yaml file with individual values to each key

我有一个 yaml 文件和 shell 脚本来编辑 yaml 文件的值。

#YAML
UI:
  repo: my-repo-1
  imageTag: ui-image:v1
DB:
  repo: my-repo-2
  imageTag: db-image:v1
#!/bin/bash
sed -i "/^\([[:space:]]*imageTag: \).*/s///" test.yaml

在 运行 脚本上用 更改 UI 的标签。/script.sh ui-image:v2正在用值“ui-image:v2”更改 UI 和 DB 的 imageTag。

我如何更改脚本,以便我可以将单个标签传递给 UI 和 DB

您可以将 </code> 的替换限制为“从 UI: 到 imageTag:”,对于 <code>DB:</code>:</p> 也是如此 <pre><code>sed -i -e "/^UI:/,/imageTag:/{/^\([[:space:]]*imageTag: \).*/s///}" \ -e "/^DB:/,/imageTag:/{/^\([[:space:]]*imageTag: \).*/s///}" \ test.yaml