【发布时间】:2020-07-01 17:31:15
【问题描述】:
我的目标是将具有单个文件的历史信息的文件系统(如嵌套目录)扁平化为 csv 文件以供进一步处理。 Here 是我迄今为止所尝试的。
简化的输入如下所示:
{ "dirs": [
{
"name": "documents",
"files": [
{
"name": "foo.bar",
"history": [
{ "hash": "123", "timestamp": "..."},
{ "hash": "234", "timestamp": "..."}
]
}
],
"subDirs": [
{ "name": "tmp", "files": [...], "subDirs": [...]
}
]
}
]}
棘手的部分是 csv 文件应该包含完整的目录路径,而不仅仅是目录名称。所需的输出如下所示:
"documents","foo.bar","123","..."
"documents","foo.bar","234","..."
"documents","bar.baz","345","..."
"documents","bar.baz","456","..."
"documents/tmp","deleteme","567","..."
"documents/tmp","deleteme","678","..."
使用recurse 扁平化大部分数据可以使用此查询:
.dirs[] | recurse(.subDirs[]?) | . as $d | $d.files[]? as $f | $f.history[]? as $h | [$d.name, $f.name, $h.hash, $h.timestamp] | @csv
...但我无法理解如何保留构建目录路径。任何建议将不胜感激。
【问题讨论】: