【问题标题】:editing yml file using command line使用命令行编辑 yml 文件
【发布时间】:2021-06-27 06:08:19
【问题描述】:

我想从命令行编辑我的 yml 文件。它在 ubuntu 服务器上。

version: "2"
services:
  samson:
    image: zendesk/samson:latest # replace with `build: .` to use Dockerfile (--build to rebuild it)
    ports:
      - "3000:9080"
    volumes:
      - .:/app/
    environment:
      DATABASE_URL: "sqlite3:///app/db/development.sqlite3"
      RAILS_LOG_TO_STDOUT: 1

我想在环境部分添加一个环境变量。我想从命令行执行此操作。我试过sed和echo。它确实被添加了,但没有正确的缩进,并且 yml 格式对语法非常敏感。

【问题讨论】:

    标签: amazon-web-services yaml


    【解决方案1】:

    您是否使用了 'yq' 而不是 'sed',因为它可以理解 yaml 格式,而 sed 将它们视为字符串,因此会给出不正确的缩进。

    【讨论】:

    • 是的,我尝试使用此命令:cat mytemplate.yml | yq -Y '.services.samson.environment += {"newKey":"newVal"}' 但我不断收到此错误 Error: unknown shorthand flag: 'Y' in -Y
    【解决方案2】:

    最简单的方法是使用yq

    cat mytemplate.yml | yq -Y '.services.samson.environment += {"newKey":"newVal"}'
    

    【讨论】:

    • 它要求我以这种格式写它yq write [yaml_file] [path_expression] [value] [flags] 我使用了这个命令yq w docker-compose.yml "services.samson[0].environment[0].value" "val"它抛出了这个错误:Error: Error reading document at index 0, yaml: line 12: could not find expected ':': yaml: line 12: could not find expected ':' Usage: yq write [yaml_file] [path_expression] [value] [flags]
    • .@sagar 我给你的命令是正确的。
    • 我试过了我得到这个错误错误:未知的速记标志:'Y' in -Y
    猜你喜欢
    • 2017-09-16
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 2010-10-30
    • 1970-01-01
    相关资源
    最近更新 更多