Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to split .csv file

I have a very large .csv file (>500mb) and I wish to break this up into into smaller .csv files in command prompt. (Basically trying to find a linux "split" function in Windows".

This has to be a batch script as my machine only has windows installed and requesting softwares is a pain. I came across a number of sample codes (http://forums.techguy.org/software-development/1023949-split-100000-line-csv-into.html), however, it does not work when I execute the batch. All I get is one output file that is only 125kb when I requested it to parse every 20 000 lines.

Has anyone ever come across a similar problem and how did you resolve the issue?

like image 554
SeekingAlpha Avatar asked Dec 16 '13 03:12

SeekingAlpha


People also ask

How do I split a CSV file in command prompt?

In Terminal, navigate to the folder you just created using the 'cd' command, which stands for 'change directory. ' Now, you'll use the 'split' command to break the original file into smaller files.


1 Answers

Try this out:

@echo off setLocal EnableDelayedExpansion  set limit=20000 set file=export.csv set lineCounter=1 set filenameCounter=1  set name= set extension= for %%a in (%file%) do (     set "name=%%~na"     set "extension=%%~xa" )  for /f "tokens=*" %%a in (%file%) do (     set splitFile=!name!-part!filenameCounter!!extension!     if !lineCounter! gtr !limit! (         set /a filenameCounter=!filenameCounter! + 1         set lineCounter=1         echo Created !splitFile!.     )     echo %%a>> !splitFile!      set /a lineCounter=!lineCounter! + 1 ) 

As shown in the code above, it will split the original csv file into multiple csv file with a limit of 20 000 lines. All you have to do is to change the !file! and !limit! variable accordingly. Hope it helps.

like image 65
Dale Avatar answered Oct 19 '22 18:10

Dale