12

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.

mespiaut
  • 123
  • 1
  • 1
  • 4

1 Answers1

22

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.

Michael Homer
  • 74,824
  • 17
  • 212
  • 233
  • 1
    Wow! I wasn't expecting such a simple answer. I was completely unaware of the `split` command. Thank you so very much! – mespiaut Jul 11 '14 at 11:30
  • Is it possible to split one file among 4 files 3 rows at a time? The rub is that the orig file may contain anywhere from 0 rows up to 45, but always divisible by 3. – Emile Nov 07 '17 at 19:41
  • @Emile I'm not sure, you should [ask a question](https://unix.stackexchange.com/questions/ask). – Michael Homer Nov 07 '17 at 19:45