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

如何移除Markdown文件中YAML的空属性?

使用mikefarah/yq移除Markdown YAML中的空属性

单个文件处理命令

yq -i 'del(.. | select((type == "seq" or type == "map") and length == 0))' Hello.md

递归处理目录下所有Markdown文件(Linux/macOS)

find /目标目录路径 -name "*.md" -exec yq -i 'del(.. | select((type == "seq" or type == "map") and length == 0))' {} \;

命令说明

  • del(.. | select(...)):递归遍历YAML结构,精准删除符合条件的节点
  • type == "seq" or type == "map":匹配空数组(seq)、空映射(map)类型的属性
  • length == 0:筛选出长度为0的空节点,确保只删除无内容的属性行
  • -i:直接修改原文件,不会破坏Markdown的YAML分隔符---

扩展处理:包含空字符串属性

如果需要同时清理类似title: 这种空字符串属性,可使用以下命令:

yq -i 'del(.. | select((type == "seq" or type == "map") and length == 0) or (type == "str" and . == ""))' Hello.md

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 23:52:03