Solved

java split csv file into multiple csv files

Posted on 2014-12-24
7
355 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
DevOps Toolchain Recommendations

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

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now