拆分大型csv文件并在每个部分中保留标题 [英] Split large csv file and keep header in each part
问题描述
如何拆分大型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屋!