【发布时间】:2019-12-26 10:28:59
【问题描述】:
我有一个 yaml 文件列表,每个文件都描述一个项目,并带有一个键 sdgs,其中包含代表可持续发展目标的数字列表。
我想合并所有文件并将它们转换为不同格式的json,以sdg索引为键,相关项目为列表值。
输入:
---
# gnu_health.yaml
description: >
GNU Health is a Free/Libre project for health practitioners, health
institutions and governments. It provides the functionality of Electronic
Medical Record (EMR), Hospital Management (HMIS) and Health Information
System (HIS).
sdgs: [3]
name: GNU Health
---
# a11y.yaml
description: >
This Accessibility Project is a community-driven effort to make web
accessibility easier by leveraging a worldwide community of developer
knowledge.
sdgs: [10]
name: A11Y
---
# bahmni.yaml
description: >
Bahmni is an Open Source hospital Management System focusing
on poor/underserved and public hospitals in the developing
world.
It's aimed to being a generic system which can be used for
multiple diseases and hospitals in different countries.
sdgs: [1, 3]
name: Bahmni
预期输出:
{
"1": [
{
"name": "Bahmni",
"description: "..."
}
],
"3": [
{
"name": "GNU Health",
"description: "..."
},
{
"name": "Bahmni",
"description: "..."
}
],
"10": [
{
"name: "A11Y",
"description: "..."
}
]
}
即使在阅读了 manual 和其他 awesome-jq 资源之后,我也发现使用 jq 的过滤系统很难解决这个问题。
有人能指出正确的方向吗?
目前的最大努力:
# use as follow: yq -f $binDir/concat_sdgs.jq $srcDir/*.y*ml
# concat_sdgs.jq
{
(.sdgs[]|tostring): [.]
}
不幸的是,这不会将来自同一个 sdg 的项目合并在一起
当前输出不正确:
{
"1": [
{
"name": "Bahmni",
"description: "..."
}
],
"3": [
{
"name": "GNU Health",
"description: "..."
}
],
"3": [
{
"name": "Bahmni",
"description: "..."
}
],
"10": [
{
"name: "A11Y",
"description: "..."
}
]
}
【问题讨论】: