Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split one text file into multiple *.txt files?

Tags:

linux

bash

I got a text file file.txt (12 MB) containing:

something1 something2 something3 something4 (...) 

Is there a way to split file.txt into 12 *.txt files, let’s say file2.txt, file3.txt, file4.txt, etc.?

like image 333
Kris Avatar asked Sep 26 '13 14:09

Kris


People also ask

How do I split a large file into smaller parts in Windows?

Right-click the file and select the Split operation from the program's context menu. This opens a new configuration window where you need to specify the destination for the split files and the maximum size of each volume. You can select one of the pre-configured values or enter your own into the form directly.


2 Answers

You can use the Linux Bash core utility split:

split -b 1M -d  file.txt file 

Note that M or MB both are OK but size is different. MB is 1000 * 1000, M is 1024^2

If you want to separate by lines you can use -l parameter.

UPDATE

a=(`wc -l yourfile`) ; lines=`echo $(($a/12)) | bc -l` ; split -l $lines -d  file.txt file 

Another solution as suggested by Kirill, you can do something like the following

split -n l/12 file.txt 

Note that is l not one, split -n has a few options, like N, k/N, l/k/N, r/N, r/k/N.

like image 160
CS Pei Avatar answered Sep 18 '22 19:09

CS Pei


$ split -l 100 input_file output_file 

where -l is the number of lines in each files. This will create:

  • output_fileaa
  • output_fileab
  • output_fileac
  • output_filead
  • ....
like image 23
amruta takawale Avatar answered Sep 19 '22 19:09

amruta takawale