简体   繁体   English

yq append 数据到 yaml 文件就地

[英]yq append data to a yaml file inplace

I have a YAML file file.yaml of this sort:我有一个 YAML 文件 file.yaml 这类:

category: fruits
some_dummy_key: value

Now i would like to use Mike farah's yq v4(version 4.5.1 to be specific) https://github.com/mikefarah/yq to append details key inplace which has a list of name, color that should look like this:现在我想使用 Mike farah 的 yq v4(具体为 4.5.1 版) https://github.com/mikefarah/yq到 append details键,其中包含名称列表,颜色应如下所示:

category: fruits
somme_dummy_key: value
details:
- name: banana
  color: yellow
- name : apple
  color: red  

In yq version, i know we could do a在 yq 版本中,我知道我们可以做一个

./bin/yq write -i file.yaml details.[0].name banana

./bin/yq write -i file.yaml details.[0].color yellow

and so on.等等。 But how do i do it using yq v4's eval command.但是我如何使用 yq v4 的 eval 命令来做到这一点。 I really appreciate any help with this.我真的很感激这方面的任何帮助。

Here is how it can be done.这是如何做到的。 You will have to create a new root value called details and assign the parameters.您将必须创建一个名为 details 的新根值并分配参数。

cbiswal@XXXXX:~$ cat tet.yaml
category: fruits
somme_dummy_key: value
cbiswal@XXXXX:~$ yq e '."details"+=[{"name":"mango","color":"yellow"},{"name":"apple","color":"red"}]' tet.yaml
category: fruits
somme_dummy_key: value
details:
  - name: mango
    color: yellow
  - name: apple
    color: red

Note that:注意:

$ yq --version
yq version 4.7.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM