使用 Miller 按列拆分巨大的 CSV
Split huge CSV by columns with Miller
我需要每天拆分每个包含 50K+ 列的巨大 (>1 Gb) CSV 文件。
我发现 Miller 是完成此类任务的有趣且高效的工具。
但我仍然停留在 Miller 的文档上。
如何将一个 CSV 拆分为 N
个更小的 CSV 文件,其中 N
是源文件中的行数?
试试这个脚本
mlr --csv put -S 'if (NR % 10000 == 0) {$rule=NR} else {$rule = ""}' \
then fill-down -f rule \
then put -S 'if ($rule=="") {$rule="0"}' \
then put -q 'tee > $rule.".csv", $*' input.csv
在新文件夹中复制您的 CSV,然后 运行 将此脚本放在上面。
它将为每 10000 行生成一个 csv 文件。
aborruso 的回答确实在输出 csv 文件中添加了一个新列 rule
。
如果您想避免这种情况,请在最后一步中使用 emit
和 mapexcept
而不是 tee
,如下所示:
mlr --csv put -S 'if (NR % 10000 == 0) {$rule=NR} else {$rule = ""}' \
then fill-down -f rule \
then put -S 'if ($rule=="") {$rule="0"}' \
then put -q 'emit > $rule.".csv", mapexcept($*, "rule")' input.csv
我需要每天拆分每个包含 50K+ 列的巨大 (>1 Gb) CSV 文件。
我发现 Miller 是完成此类任务的有趣且高效的工具。
但我仍然停留在 Miller 的文档上。
如何将一个 CSV 拆分为 N
个更小的 CSV 文件,其中 N
是源文件中的行数?
试试这个脚本
mlr --csv put -S 'if (NR % 10000 == 0) {$rule=NR} else {$rule = ""}' \
then fill-down -f rule \
then put -S 'if ($rule=="") {$rule="0"}' \
then put -q 'tee > $rule.".csv", $*' input.csv
在新文件夹中复制您的 CSV,然后 运行 将此脚本放在上面。 它将为每 10000 行生成一个 csv 文件。
aborruso 的回答确实在输出 csv 文件中添加了一个新列 rule
。
如果您想避免这种情况,请在最后一步中使用 emit
和 mapexcept
而不是 tee
,如下所示:
mlr --csv put -S 'if (NR % 10000 == 0) {$rule=NR} else {$rule = ""}' \
then fill-down -f rule \
then put -S 'if ($rule=="") {$rule="0"}' \
then put -q 'emit > $rule.".csv", mapexcept($*, "rule")' input.csv