Solved

java split csv file into multiple csv files

Posted on 2014-12-24
7
378 Views
Last Modified: 2015-01-07
Hi

I have a csv file with 4003 lines and the file name is "testnow.csv"
I want to split this csv file into 41 csv files, I mean each csv file cannot have more than 100 lines
and the new csv file name should be somthing like this

testnow_1.csv
testnow_2.csv
testnow_3.csv
.....

testnow_41.csv

Thanks,
0
Comment
Question by:shragi
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 40516878
What's your OS?
0
 

Author Comment

by:shragi
ID: 40516937
Windows
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 40516993
my $num = 1;
open(my $csv_in, '<', 'testnow.csv') or die "failed to open testnow.csv <$!>";
open(my $csv_out, '<', "testnow_$num.csv") or die "failed to open testnow_$num.csv <$!>";

while (my $line = <$csv_in>) {
    print $csv_out $line;
    if (0 == $. % 100) {
        close $csv_out;
        $num++;
        open($csv_out, '<', "testnow_$num.csv") or die "failed to open testnow_$num.csv <$!>";
    }
}
close $csv_out;
close $csv_in;

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:shragi
ID: 40519023
@FishMOnger
small problem with your script, in order to use your script the files should be readily created before hand.
But is it possible to create the file along with the other logic.


Thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 40519033
You said you have a csv file with 4003 lines
But if you need to create it, that can be done if you have the logic to generate the contents.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40519556
>>in order to use your script the files should be readily created before hand.

That's not the case. Did you try it?
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 40519595
@CEHJ
The code I gave does require the input csv file to already exist but not the output file.

I just noticed that I have a typo in the code which will cause it to fail.  I opened the output file in read mode, which certainly is not what is wanted.

Change:
open(my $csv_out, '<', "testnow_$num.csv")

Open in new window


To:
open(my $csv_out, '>', "testnow_$num.csv")

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question