拆分csv文件分成几部分,复制标题 [英] Split a csv file into parts, copy header

查看:188
本文介绍了拆分csv文件分成几部分,复制标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的快捷方式到CSV文件分成N个大致相等的部分,使每一部分都有原始标题作为第一行。
到目前为止,我想出了这一点,其中工程(N = 5):

I want a quick way to split a csv file into N roughly equal parts and make each part have the original header as the first line. So far I've come up with this, which works (N=5):

split -da 4 -l $((`wc -l < foo.csv`/5)) foo.csv foo --additional-suffix=".csv"
for f in `ls foo0*.csv`;do sed -i "1s/^/`head -n 1 foo.csv`\n/" $f; done;

有谁有关于如何把它变成一个班轮,我被分养活新创建的文件转换成for循环,而不是运行ls命令的想法。

Does anybody have an idea on how to turn this into a one-liner where I feed the newly created files by split into the for loop instead of running the ls command.

谢谢!

推荐答案

您不需要一个for循环的。你可以简单地做到这一点:

You don't need a for loop at all. You can simply do this:

sed -i "1s/^/`head -n 1 foo.csv`\n/" foo0*.csv

我不认为你可以通过 SED 饲料新创建的文件,因为拆分不给你新的文件名。

I don't think you can feed the newly created files through sed because split doesn't give you the new filenames.

如果你真的想要一个单行,你可以加入与两个语句&放大器;&安培;

If you really want a one-liner, you can join the two statements with &&:

split -da 4 -l $((`wc -l < foo.csv`/5)) foo.csv foo --additional-suffix=".csv" && sed -i "1s/^/`head -n 1 foo.csv`\n/" foo0*.csv

这篇关于拆分csv文件分成几部分,复制标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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