带有空格的sed捕获组不起作用
我想用sed更新kubernetes kustomization.yaml中的标记。原始文件如下所示:带有空格的sed捕获组不起作用,sed,yaml,Sed,Yaml,我想用sed更新kubernetes kustomization.yaml中的标记。原始文件如下所示: resources: - ../../base namePrefix: prod- commonLabels: env: production images: - name: my-service newTag: current-version patchesStrategicMerge: - deployment.yaml 当我使用sed命令时,它不起作用,我不知道
resources:
- ../../base
namePrefix: prod-
commonLabels:
env: production
images:
- name: my-service
newTag: current-version
patchesStrategicMerge:
- deployment.yaml
当我使用sed命令时,它不起作用,我不知道为什么:
sed -r 'name: my-service\s*(newTag:\s*).*/\1new-version/g' overlays/production/kustomization.yaml
据我所知,如果在名称:我的服务
元素之后,它应该与newTag
键匹配。我没有收到任何错误,只是不起作用
我目前正在MacOS上测试这一点,
yq
将是一个合适的处理工具
yaml
文件。如果yq
可用,请尝试:
yq -y '(.images[] | select(.name == "my-service") | .newTag) |= "new-version"' yourfile.yaml
输出:
resources:
- ../../base
namePrefix: prod-
commonLabels:
env: production
images:
- name: my-service
newTag: new-version
patchesStrategicMerge:
- deployment.yaml
如果yq
不可用,并且您有使用sed
的特定原因,请尝试以下替代方法:
sed -E '
/my-service/{ ;# if the line matches "my-service", then execute the block
N ;# append the next line to the pattern space
s/(newTag:[[:space:]]*).*/\1new-version/ ;# replace the value
} ;# end of the block
' yourfile.yaml
sed
命令不起作用的原因是sed
是一个
面向行的工具,逐行处理输入。你的正则表达式是交叉的
行和将不匹配。专家建议使用
yq
(非常了解yaml文件编辑)之类的工具进行yaml编辑,如果您的系统中安装了yq
,或者您可以安装它,那么您可以添加yq
标签,以获得有关yaml编辑的指导,谢谢,学习愉快。您的sed命令起作用了。我不想使用yq的原因是它没有预先安装在我的构建映像中,我也不想创建我自己的。