【问题标题】:Split huge CSV by columns with Miller使用 Miller 按列拆分巨大的 CSV
【发布时间】:2019-09-05 05:14:03
【问题描述】:

我需要每天拆分包含 50K+ 列的巨大 (>1 Gb) CSV 文件。

我发现Miller 是完成此类任务的有趣且高效的工具。

但我被米勒的文档困住了。

如何将一个 CSV 拆分为 N 较小的 CSV 文件,其中 N 是我的源文件中的许多行?

【问题讨论】:

    标签: csv miller


    【解决方案1】:

    试试这个脚本

    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 文件。

    【讨论】:

    【解决方案2】:

    aborruso 的答案确实在输出 csv 文件中添加了一个新列 rule。 如果您想避免这种情况,请在最后一步中使用emitmapexcept 而不是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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      相关资源
      最近更新 更多