Auto split CSV file in chunks with Mac OS X Terminal

And add an extension to them also, so Mac OS X knows how (with which program) to handle them.


How to split a large text file into parts of your choosing:

  • by line number;
  • by file size (Kb or Mb);
  • or even based on an arbitrary regular expression (how cool is that?).

Example: auto split one large CSV file into files of 200 lines each

Let's say I have a 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 {example}.csv

Where '{example}.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, but it doesn't add an extension.

If you want to add an extension to the new files (for example: '.csv'), you can do that manually in Finder (by selecting all files, and change the name), or do it automagically with another Terminal command:

for i in *; do mv "$i" "$i.txt"; done

Make sure you got this right

You should know this, but just to make sure:

  1. open the Terminal in the right folder;
  2. make sure that it's ok to add the extensions to all files in the folder;
  3. and last but not least: make sure that Mac OS X can open and read the original .CSV file;

TL;DR version

TL;DR version: how to autosplit a large .CSV file into smaller files


Care to respond?

Thank you! You can use your fave social network:

Or respond to me personally. For example by calling me: +31626414088, via Skype, or .(JavaScript must be enabled to view this email address).