Solved

java split csv file into multiple csv files

Posted on 2014-12-24
7
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

734 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