带有空白的sed捕获组不起作用



我想用sed更新kubernetes kustomization.yaml中的一个标记。原始文件如下所示:

resources:
- ../../base
namePrefix: prod-
commonLabels:
env: production
images:
- name: my-service
newTag: current-version
patchesStrategicMerge:
- deployment.yaml

当我使用sed命令时,它就是不起作用,我不知道为什么:

sed -r 'name: my-services*(newTag:s*).*/1new-version/g' overlays/production/kustomization.yaml

据我所知,如果它遵循name: my-service元素,那么它应该与newTag密钥匹配。我没有任何错误,只是不起作用。

我目前正在MacOS 上测试

作为RavinderSingh13注释,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是面向行的工具,并逐行处理输入。您的正则表达式交叉行和将不匹配。

最新更新