我有这个 YAML 文件:
openapi: 3.0.0
info:
title: Test
description: >-
This is the official REST API documentation for all available routes
provided by the **Test** plugin.
version: 2.0.0
servers:
- url: https://entwicklungsumgebung
components:
securitySchemes:
basicAuth:
type: http
在我的 CI 管道中,我尝试修改第 7 行的版本。在文件的中间,有多个名为 test_version
或 prod_version
的键,它们不应更换。为此,我编写了这个 sed
命令:
sed -i '' 's/^version: .*/version: 3.0.0/' test.yml
最初,我在 s/
之后没有使用 ^
,但这也匹配版本之前的所有内容。现在,什么都没有被取代。知道我做错了什么吗?
最佳答案
您必须保留空格,因此您必须匹配整行并使用捕获组:
sed -i '' 's/^\([[:blank:]]*version:\) .*/\1 3.0.0/' test.yml
或者您可以只匹配版本
,但存在匹配不应该匹配的地方的风险:
sed -i '' 's/\(version:\) .*/\1 3.0.0/' test.yml
更强大的解决方案是使用可以解析 YAML 的工具,例如 yq :
yq -i '.info.version = "3.0.0"' test.yml
关于ubuntu - 替换 YAML 文件中的版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75463430/