我有一个类似的YAML文件
---
apps_membership:
children:
apps_prometheus:
children:
group_1:
group_2:
apps_logstash:
children:
group_3:
group_4:
apps_alertmanager:
children:
group_1:
group_2:
当我寻找vm时,我会尝试获得组,例如,我寻找group_1我想获得
apps_prometheus
apps_alertmanager
我正在尝试将kislyuk/yq和jq与map/key/contents 相结合
-----使用路径命令进行测试
如果我使用命令yq -r '.apps_membership.children | paths'
我得到这个结果:
[
"apps_prometheus"
]
[
"apps_prometheus",
"children"
]
[
"apps_logstash"
]
[
"apps_logstash",
"children"
]
[
"apps_alertmanager"
]
[
"apps_alertmanager",
"children"
]
如果我用换yaml
apps_alertmanager:
children:
group_1: ""
group_2: ""
我得到想要的输出:
[
"apps_alertmanager"
]
[
"apps_alertmanager",
"children"
]
[
"apps_alertmanager",
"children",
"group_1"
]
[
"apps_alertmanager",
"children",
"group_2"
]
如何使用命令路径也获取null值?
在kislyuk/yq中,您可以使用jq
中可用的paths
函数来筛选密钥名称
yq -r '.apps_membership.children | paths | select(.[-1] == "group_1")[0]'