How to split (large) .CSV files in macOS Terminal
You can use the split
command to split by number of lines or number of bytes.
Example 1: multiple files of 200 lines each #
split -l 200 split_me
Where 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.
Heads up!
Make sure you execute this command in the RIGHT folder, or risk changing way more files than you’ve intended!
Example: files of 1 MB each, and ending with a .csv extension #
Let's say:
-
-
I want to split a file of 6.4MB into files of 1MB each,
-
-
and I want each resulting files to also have the
.csv
extension.
For that, we need to expand 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 1m
.
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