我正在尝试替换标签值,但 sed 没有运气
这是文件的样子,我想唯一地替换标签值
app1:
tag: "dev-0.114"
repository: kafka
pullPolicy: Always
app2:
tag: "dev-0.59"
repository: nginx
pullPolicy: Always
这就是我尝试过的
sed -i "" "/^\(app1.*[[:space:]]*tag: \).*/s//\1dev-0.115/" values.yaml
但不工作
答案1
使用from进行就地编辑,将值更改.app1.tag
为 stringdev-0.115
yq
https://github.com/kislyuk/yq(这是一个jq
包装器):
$ cat file.yml
app1:
tag: "dev-0.114"
repository: kafka
pullPolicy: Always
app2:
tag: "dev-0.59"
repository: nginx
pullPolicy: Always
$ yq -i -y '.app1.tag = "dev-0.115"' file.yml
$ cat file.yml
app1:
tag: dev-0.115
repository: kafka
pullPolicy: Always
app2:
tag: dev-0.59
repository: nginx
pullPolicy: Always
使用yq
某些Linux系统上的方法,将文档转换为JSON,在 中进行修改jq
,然后将其转换回来并写入文件newfile.yml
:
$ yq -j r file.yml | jq '.app1.tag = "dev-0.115"' | yq -P r - >newfile.yml
$ cat newfile.yml
app1:
tag: dev-0.115
repository: kafka
pullPolicy: Always
app2:
tag: dev-0.59
repository: nginx
pullPolicy: Always
答案2
您可以阅读n使用命令将 ext 行插入模式空间n
。所以给出
$ cat file.yaml
app1:
tag: "dev-0.114"
repository: kafka
pullPolicy: Always
app2:
tag: "dev-0.59"
repository: nginx
pullPolicy: Always
然后
$ sed '/^app1:/{n;s/tag:.*/tag: "dev-0.115"/;}' file.yaml
app1:
tag: "dev-0.115"
repository: kafka
pullPolicy: Always
app2:
tag: "dev-0.59"
repository: nginx
pullPolicy: Always
答案3
对于此任务,我将使用最简单的可用工具:
tr -d '"' < file.yml