How to slice a text file into several files of 100 lines each?

My question might be quite unclear here.
I have a giant text file with values that looks like this

0.00601233
0.000139403
0.000133679
0.000139497
0.000141683
0.000141888
0.000138646
0.000133465
0.000146326
0.000135611
...

And I want to slice it into several files of 100 lines.

File 1 will have lines 1 to 100.

File 2 will have lines 101 to 200.

etc…

There’s probably a way to do this using sed or awk but I’m not familiar enough with theses tools or with regular expressions to do what I want here.

Asked By: mespiaut

||

Use the split command:

split -l 100 file

By default split makes output files xaa, xab, and so on, but you can specify the prefix at the end, and get purely numeric suffixes if you want:

split -d -l 100 file PREFIX 

This command will make files PREFIX01, PREFIX02, and so on. The -d option is a GNU extension, so it isn’t supported on all systems. In that case, or alternatively, you can rename them with a simple shell loop after the fact – it always generates filenames that sort in order.

Answered By: Michael Homer
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.