ubuntu - 替换 YAML 文件中的版本

标签 ubuntu sed gitlab

我有这个 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_versionprod_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/

相关文章:

ubuntu - 无法在 Ubuntu 22.04 中拖放

mysql - 使用 Ubuntu 使用 Apache2 mysql 在 VPS 上部署 Django

linux - 在unix中为两个字段添加双引号

java - 如何修复调用失败来自服务器 : Unauthorized in Android studio 的意外响应

c++ - "g++"和 "c++"编译器

ruby-on-rails - 我使用 Nginx 在 Ubuntu 上运行的 VPS 崩溃了,无法再次运行

bash - sed 用字符替换空行

linux - sed 不从文件中删除换行符

GitLab CI/CD 管道,部署到 Windows Server

git - 如何同步gitlab和github