这可能与我发布的另一个问题有关:yq (GO/Mike Farah) sort all arrays recursively?
Mike Farah 的 yq
为 making arrays unique 提供了文档但我无法弄清楚如何将其应用于嵌套更深的列表
输入
classes:
driver:
fields:
- height
- age
- age
vehicle:
fields:
- model
- model
- color
- year
期望的输出
classes:
driver:
fields:
- age
- height
vehicle:
fields:
- color
- model
- year
天真地尝试全局统一
cat to_sort.yaml | yq 'unique'
Error: Only arrays are supported for unique
如果需要参数,我不知道该提供什么。我不想只对一个显式路径进行排序,但我确实尝试过这个:
cat to_sort.yaml | yq 'unique(.classes.driver.fields)'
Error: Bad expression, please check expression syntax
我见过一些必须首先执行选择操作的 yq
示例,但我不知道在这种情况下该尝试什么。
最佳答案
yq e '(... | select(type == "!!seq")) |= unique' input
将递归循环遍历所有项目,并且select()
那些 type
!!seq
然后更新(|=
)那些具有unique
的:
提供输入的结果:
classes:
driver:
fields:
- height
- age
vehicle:
fields:
- model
- color
- year
关于arrays - yq (GO/Mike Farah) 递归统一所有数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73746733/