如何用yq递归筛选含指定键的RunningProcess映射的YAML文件
解决方案
结合find和yq v4.35.2,可以用以下命令递归筛选符合条件的.yaml文件路径:
find . -type f -name "*.yaml" -exec yq e 'any(.. | select(tag == "!!map" and key == "RunningProcess"); . | has("substring") or has("export") and (has("CmdLine") | not) and (has("regex") | not))' {} \; -print
命令说明
find . -type f -name "*.yaml":递归查找当前目录下所有.yaml格式的文件-exec yq e '...' {} \;:对每个找到的文件执行yq检查逻辑any(.. | select(tag == "!!map" and key == "RunningProcess"); ...):遍历文件所有层级,筛选出名为RunningProcess的映射节点,只要有一个节点满足后续条件就返回true. | has("substring") or has("export"):检查该RunningProcess映射是否包含substring或export键(has("CmdLine") | not) and (has("regex") | not):确保该映射同时不包含CmdLine和regex键
-print:当yq检查返回true时,输出当前文件的完整路径
补充说明
如果你的yq是通过brew等包管理器安装的,部分环境可能需要用yq eval代替yq e,不过v4.35.2版本中e是eval的简写,两者功能完全等效。
内容的提问来源于stack exchange,提问作者Shane Bishop
相关产品推荐
相关产品推荐

