使用 yq 清理 YAML 前端内容 (macOS)
我有大约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要传递
--front-matter=process
修改front-matter内容时,传入修改标签所需的表达式。对于情况 1) 仅需要排序的预格式化数组表示法,请使用 yq 作为
情况 2) 在
上需要拆分,
执行如下操作。flow
样式将数组内容放在[..]
中对于情况 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
For case 2) splitting needed on
,
do it as below. Theflow
style puts the array contents within the[..]
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.