You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac下使用yq v4如何提取带下划线前缀的键及其内容?

用yq提取以下划线开头的键值对

环境与原数据

  • 系统:Mac OSX
  • yq版本:v4.35.2

原YAML数据(test.yml):

_format_version: "1.1"
_info:
  select_tags:
    - test
_workspace: test
plugins:
  - blah: abc
routes: 
  - blah: def
services: 
  - blah: ghi

需求

提取所有以下划线开头的键及其对应内容,预期输出:

_format_version: "1.1"
_info:
  select_tags:
    - test
_workspace: test

正确命令

使用with_entries结合select和startswith筛选目标键:

yq 'with_entries(select(.key | startswith("_")))' test.yml

命令解释

  • with_entries:遍历顶层所有键值对,对每个条目进行处理
  • select(.key | startswith("_")):筛选出键(.key)以_开头的条目,保留这些键值对

执行该命令后即可得到符合预期的输出,不会出现仅返回值或误删目标键的问题。

内容的提问来源于stack exchange,提问作者MJFrog14

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:15:54