6

这里用 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

执行

yq -y . input.yml

结果

command: fish -l
cwd: null
env:
  recording: true
cols: 110
4

1 回答 1

4

在某些有限的情况下,您可以将 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

创建一个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
于 2019-09-14T18:27:42.810 回答