使用 yq 清理 YAML 前端内容 (macOS)

发布于 2025-01-10 08:10:59 字数 1050 浏览 0 评论 0原文

我有大约 30,000 个 .md 文件,其中包括 frontmatter。 他们都有不同的钥匙组。为了按字母顺序对它们进行排序,我成功地使用了brew yq 和find。

find ./ -type f -name "*.md" -exec yq -i -f process 'sort_keys(.)' "{}" \;

现在我需要清理标签,这些标签具有接下来的三种形式之一

---
author: Karen the Trollmaster
tags: [legal, business, security]
tags: legal, business, security
tags: legal,business/entrepreneurship security
---

,我需要按字母顺序对它们进行排序,并将它们从字符串转换为

tags: [business/entrepreneurship, legal, security]

我尝试过的

yq '.tags | sub("," , " ") | split(" ")' something.md

正确数组,

yq '.tags | sub("," , " ") | map(split(" "))' something.md

但我得到的只是一个空数组,或者一个错误说可以't 替换为 !!null

所需的输出将是一个不带“”的数组

tags: [business/entrepreneurship, legal, security, watermelons]

I have around 30,000 .md files which include a frontmatter.
all of them have different set of keys. To sort them alphabetically I successfully used brew yq and find.

find ./ -type f -name "*.md" -exec yq -i -f process 'sort_keys(.)' "{}" \;

Now I need to sanitize tags, which have one of the next three forms

---
author: Karen the Trollmaster
tags: [legal, business, security]
tags: legal, business, security
tags: legal,business/entrepreneurship security
---

I need to sort them alphabetically, and convert them from string to a proper array

tags: [business/entrepreneurship, legal, security]

I tried

yq '.tags | sub("," , " ") | split(" ")' something.md

and

yq '.tags | sub("," , " ") | map(split(" "))' something.md

but all i'm getting is an empty array, or an error saying can't substitute with !!null

The desired output would be as an array without ""

tags: [business/entrepreneurship, legal, security, watermelons]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住你手 2025-01-17 08:10:59

您需要传递 --front-matter=process 修改front-matter内容时,传入修改标签所需的表达式。

对于情况 1) 仅需要排序的预格式化数组表示法,请使用 yq 作为

yq e -f process '.tags |= sort' yaml

情况 2) 在 上需要拆分, 执行如下操作。 flow 样式将数组内容放在 [..]

yq e -f process '.tags |= (split(", ") | sort | . style="flow") ' yaml

对于情况 3),不清楚输入是否有效作为强制分隔符 , 不作为输入的一部分出现。如果适用,请使用案例 2 的解决方案。

注意:如果您使用 yq 版本 4.18.1 或更高版本,不再需要评估标志e,因为它已成为默认操作。

You need to pass the --front-matter=process when modifying the front-matter content and pass the required expression to modify the tags.

For case 1) pre-formatted array notation with just sorting needed, use yq as

yq e -f process '.tags |= sort' yaml

For case 2) splitting needed on , do it as below. The flow style puts the array contents within the [..]

yq e -f process '.tags |= (split(", ") | sort | . style="flow") ' yaml

For case 3) it is not clear if the input is valid as the mandatory delimiter , is not present as part of one the inputs. Use the solution for case 2 as applicable.

Note: If you are using yq version 4.18.1 or beyond, the eval flag e is no longer needed as it has been made the default action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文