简体   繁体   English

yq append 多个文件进入列表 yaml

[英]yq append multiple file into list yaml

Assume I have a file root.yml假设我有一个文件root.yml

keyA: valA
keyB: valB
myList:

Then I receive some yml file, such as然后我收到一些 yml 文件,例如

1.yml

project_id: abc
description: xyz

2.yml

project_id: cba
description: zyx

And so on (they may stored in same folder)依此类推(它们可能存储在同一个文件夹中)

Now I want to append the content of 1.yml , 2.yml (and so on) to the myList of root.yml and output to console现在我想 append 的内容1.yml2.yml (等等)到myListroot.yml和 output 到控制台

Expected:预期的:

keyA: valA
keyB: valB
myList:
  - project_id: abc
    description: xyz
  - project_id: cba
    description: zyx
  - (so on...)

I have searched some examples but they hard code the list item in the yq command, like this post: Stack Overflow我已经搜索了一些示例,但它们对 yq 命令中的列表项进行了硬编码,例如这篇文章: Stack Overflow

But I want it load from files, not from hard code Please forgive for my bad english但我希望它从文件中加载,而不是从硬代码中加载请原谅我的英语不好

With mikefarah/yq , you could use the load function with the filename of the YAML files to be included, ie with your example使用mikefarah/yq ,您可以使用加载 function 与要包含的 YAML 文件的文件名,即与您的示例

yq '.myList += [ load("1.yaml"), load("2.yaml") ]' root.yml

producing a YAML result as产生 YAML 结果为

keyA: valA
keyB: valB
myList:
  - project_id: abc
    description: xyz
  - project_id: cba
    description: zyx

As indicated in your comment , if one of the object has a parent structure and you want to extract the element from it, you can do如您的评论中所示,如果 object 之一具有父结构并且您想从中提取元素,您可以执行

yq 'load("1.yaml") as $f | .myList += [ $f.config[], load("2.yaml") ]' root.yml

Tested on yq version 4.27.2yq版本 4.27.2 上测试

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

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