如何使用 yq 搜索 yml 文件中的键中是否存在特定单词

我最近刚开始使用 yq 工具,一直在努力寻找一种方法来检查 yml 文件的键中是否存在特定单词。

说yml文件如下,

key1:
  value1
key_with_test:
  value2
key3:
  value3

在这种情况下,我想搜索它们是否是带有“test”一词的键。

我尝试了以下命令,

yq eval "keys | has("test")"
yq eval "keys | select(. == "*test")"

但似乎没有按预期工作。如果存在,预计是一个布尔值,如果可能的话,将来如何访问它。

stack overflow How to search if a particular word exists in keys in yml file using yq
原文答案
author avatar

接受的答案

yq uses a different syntax than jq to get elements from the array. In the latter you could just do .arr[], but former expects .arr | .[]. So putting it together

yq e 'keys | .[] | select(. == "*test")' yaml

Also note that has does not support a regex/glob search but only for exact match string.

Note: Since 4.18.1, yq's eval/e command is the default command and no longer needs to be specified.


答案: