Our blog
Linux Split File (eg CSV) and Keep Header Row
Linux has a great little utility called split, which can take a file and split it into chunks of whatever size you want, eg 100 line chunks.
However, for CSV files etc, each chunk generally needs to have the header row in there. Unfortunately the split command doesn't have an option for that. However with a little bit more code you can achieve it.
-
tail -n +2 file.txt | split -l 4 - split_
-
for file in split_*
-
do
-
head -n 1 file.txt> tmp_file
-
cat $file>> tmp_file
-
mv -f tmp_file $file
-
done
More Reading:
One Comments
|
note, if the file has line breaks in cell content the split struggles.. I ended up abandoning it |
RSS Feed
admin
October 28th, 2009