这里使用 yq 编辑数组中的 yaml 对象。加速 Terminalizer 的终端转换(记录)我问过如何用 yq 编辑 yaml。我得到了最好的答案。但默认情况下yq
会删除注释和空行。如何防止这种行为?
input.yml
# Specify a command to be executed
# like `/bin/bash -l`, `ls`, or any other commands
# the default is bash for Linux
# or powershell.exe for Windows
command: fish -l
# Specify the current working directory path
# the default is the current working directory path
cwd: null
# Export additional ENV variables
env:
recording: true
# Explicitly set the number of columns
# or use `auto` to take the current
# number of columns of your shell
cols: 110
Run Code Online (Sandbox Code Playgroud)
执行
yq -y . input.yml
Run Code Online (Sandbox Code Playgroud)
结果
command: fish -l
cwd: null
env:
recording: true
cols: 110
Run Code Online (Sandbox Code Playgroud)
在某些有限的情况下,您可以将 diff/patch 与 yq 一起使用。
例如,如果input.yml
包含您的输入文本,则命令
$ yq -y . input.yml > input.yml.1
$ yq -y .env.recording=false input.yml > input.yml.2
$ diff input.yml.1 input.yml.2 > input.yml.diff
$ patch -o input.yml.new input.yml < input.yml.diff
Run Code Online (Sandbox Code Playgroud)
创建一个input.yml.new
保留注释但记录更改为 false 的文件:
# Specify a command to be executed
# like `/bin/bash -l`, `ls`, or any other commands
# the default is bash for Linux
# or powershell.exe for Windows
command: fish -l
# Specify the current working directory path
# the default is the current working directory path
cwd: null
# Export additional ENV variables
env:
recording: false
# Explicitly set the number of columns
# or use `auto` to take the current
# number of columns of your shell
cols: 110
Run Code Online (Sandbox Code Playgroud)