How to split large CSV files in Mac OS X Terminal, the easy way. And add an extension to them also, so Mac OS X knows how to handle them.
How to split a large text file into parts of your choosing:
Let's say I have one .csv-file that I want to split into multiple files that contain a maximum of 200 lines. I can use the following command:
split -l 200 split_me
split_me.csv is the filename of the CSV file you wish to split into chunks of 200 lines. The split function itself is smart enough to give the new files a name, so no need to worry about that.
For many cases this alone is enough. But if you want some additional options, read on.
Let's make it more interesting:
For that, we need to alter the split command a little bit.
First we used the
-l flag, which separated the files by a number of lines. Now we're going to use the
-b flag, which separates by size (bytes). Splitting it in files of 1MB is easy, just use
And additionally, we'll use a little loop and the
mv-command to change the extensions.
First: split into chunks of 1mb each:
split -b 1m split_me.csv
And next, add the
.csv extension to all of them:
for i in *; do mv "$i" "$i.csv"; done
Et voila! We can see the result:
You should know this, but just to make sure.
Not much really. But if you think this was useful to you, it might be for others also: