I’ve got a large (by number of lines) plain text file that I’d like to split into smaller files, also by number of lines. So if my file has around 2M lines, I’d like to split it up into 10 files that contain 200k lines, or 100 files that contain 20k lines (plus one file with the remainder; being evenly divisible doesn’t matter).
I could do this fairly easily in Python, but I’m wondering if there’s any kind of ninja way to do this using Bash and Unix utilities (as opposed to manually looping and counting / partitioning lines).
Have a look at the split command:
You could do something like this:
which will create files each with 200000 lines named
xaa xab xac…Another option, split by size of output file (still splits on line breaks):
creates files like
output_prefix01 output_prefix02 output_prefix03 ...each of maximum size 20 megabytes.