Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell command to split large file into 10 smaller files

Tags:

shell

cygwin

I have a csv import file with 33 million lines that need to be imported into my database. I can import it with a C# console app but then the stored procedures that run after the import timeout. Consequently I want to split the file into 10 smaller files.

I could do it in C# but I suspect there's a much better approach using shell utilities. I have cygwin installed and can use all the common Linux shell utilities. Is there a neat little combination of commands I could use to split the file?

like image 235
sipsorcery Avatar asked Nov 08 '10 21:11

sipsorcery


People also ask

How do I split a text file into multiple files in Shell?

To split a file into pieces, you simply use the split command. By default, the split command uses a very simple naming scheme. The file chunks will be named xaa, xab, xac, etc., and, presumably, if you break up a file that is sufficiently large, you might even get chunks named xza and xzz.

How do I split a large file into multiple smaller files?

Use the location bar to navigate to the folder that contains the large file on your system. 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.

How do you split a large file into smaller parts in Unix?

If you use the -l (a lowercase L) option, replace linenumber with the number of lines you'd like in each of the smaller files (the default is 1,000). If you use the -b option, replace bytes with the number of bytes you'd like in each of the smaller files.

How do I split a large file into multiple files in Linux?

To split a file equally into two files, we use the '-n' option. By specifying '-n 2' the file is split equally into two files.


1 Answers

Use split - e.g. to split a file every 3.4 million lines (should give you 10 files):

split -l 3400000

$ man split

like image 178
Paul R Avatar answered Sep 18 '22 08:09

Paul R