December 7, 2010

How to split a file

I often have a file that's too big. A great way to split the file is to use.....wait for it.....the split command. I usually split a file by line count. Here's an example invocation:

mike@shiner $ split -l 100 -d test_file.txt test_file_

The -l option defines the maximum number of lines per file. If 'test_file.txt' has 951 lines, then we'll end up with 10 files where we have 9 with 100 lines each and 1 with 51 lines. The -d option tells split to use numeric suffixes. 'test_file.txt' is the file we're going to split and 'test_file_' is the prefix used for each of the files.

mike@shiner $ ls
test_file.txt  test_file_02  test_file_05  test_file_08
test_file_00   test_file_03  test_file_06  test_file_09
test_file_01   test_file_04  test_file_07

No comments:

Post a Comment