拆分大型csv文件并在每个部分中保留标题 [英] Split large csv file and keep header in each part

查看:311
本文介绍了拆分大型csv文件并在每个部分中保留标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何拆分大型csv文件(〜100GB)并保留每个部分的标题?

How to split a large csv file (~100GB) and preserve the header in each part ?

例如

h1 h2
a  aa
b  bb


$ b b

变成

into

h1 h2
a  aa

h1 h2
b  bb


推荐答案

首先,您需要分隔标题和内容:

First you need to separate the header and the content :

header=$(head -1 $file)
data=$(tail -n +2 $file)



然后您要拆分数据

Then you want to split the data

echo $data | split [options...] -

在选项中,您必须指定块的大小结果文件的名称的模式。不要删除尾部 - ,因为它指定 split 从stdin读取数据。

In the options you have to specify the size of the chunks and the pattern for the name of the resulting files. The trailing - must not be removed as it specifies split to read data from stdin.

然后,您可以在每个文件的顶部插入标题

Then you can insert the header at the top of each file

sed -i "1i$header" $splitOutputFile

你应该明显地在for循环中做最后一部分,取决于为拆分操作选择的前缀。

You should obviously do that last part in a for loop, but its exact code will depend on the prefix chosen for the split operation.

这篇关于拆分大型csv文件并在每个部分中保留标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆