如何防止yq删除注释和空白行?

时间:2019-08-23 13:20:54

标签: comments jq yq

这里Edit yaml objects in array with yq. Speed up Terminalizer's terminal cast (record)我问过如何使用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

1 个答案:

答案 0 :(得分:1)

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