【发布时间】:2019-12-28 20:02:27
【问题描述】:
这里Edit yaml objects in array with yq. Speed up Terminalizer's terminal cast (record)问了怎么用yq编辑yaml。我收到了最好的答案。但默认情况下yq 会删除 cmets 和空行。如何防止这种行为?
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
【问题讨论】:
-
yq的开发者好像还没有添加这个功能-github.com/mikefarah/yq/issues/19 -
Kyb - 看了几个 yaml2json 工具后,我得出的初步结论是,不仅任何此类工具都没有解决问题,而且通用和可逆解决方案的难度大概是FOSS的范畴之外了。换句话说,您可能会更幸运地直接修改 YAML。
-
@peak,我也发现了。
sed是一个超级通用的工具。 -
问题是大多数这样的工具不能直接处理文本;它们使用由解析文本生成的抽象语法树,并且在解析过程中通常会删除 cmets。一般来说,如果输出与输入不同,您会在哪里“重新插入”任何已保存的 cmets?