1

我有一个格式如下的文件(嗯,有点):

RECORD_SEPARATOR
foo: some foo value
bar: another value
baz: 123
RECORD_SEPARATOR
foo: another foo value
bar: yet another value
baz: 345
RECORD_SEPARATOR
foo: a third foo
RECORD_SEPARATOR
bar: a fourth bar
baz: 111

等等。这里的关键点是并非所有记录都存在所有字段。

我的问题:将这些数据转换为 CSV 格式的超级简单方法是什么?也就是说,在我的例子中

foo,bar,baz
some foo value,another value,123
another foo value,yet another value,345
a third foo,,
,a fourth bar,111

当然,您可以为此编写一个 awk(或 perl 或 Python)脚本,但我希望有一些预先存在的东西,或者一些技巧可以使它成为一个非常短的脚本。

注意:我正在寻找的东西当然是面向 Unix 命令行的。

4

1 回答 1

2

嗨,伟大的米勒http://johnkerl.org/miller/doc,从

foo: some foo value
bar: another value
baz: 123

foo: another foo value
bar: yet another value
baz: 345

foo: a third foo

bar: a fourth bar
baz: 111

你可以跑

mlr --x2p --ips ": " --barred cat then unsparsify --fill-with "" inputFile

并有这个漂亮的打印输出

+-------------------+-------------------+-----+
| foo               | bar               | baz |
+-------------------+-------------------+-----+
| some foo value    | another value     | 123 |
| another foo value | yet another value | 345 |
| a third foo       | -                 | -   |
| -                 | a fourth bar      | 111 |
+-------------------+-------------------+-----+

如果你想要一个 CSV,运行

mlr --x2c --ips ": " cat then unsparsify --fill-with "" inputFile

你会有

foo,bar,baz
some foo value,another value,123
another foo value,yet another value,345
a third foo,,
,a fourth bar,111
于 2019-01-22T22:46:11.630 回答