Solved

java split csv file into multiple csv files

Posted on 2014-12-24
7
436 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
Technology Partners: 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!

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to print sub line with  sed /awk /perl in bash file 11 62
Which non-HTML GUI front end to use with Java? 3 52
java example issue 5 42
wild fly 8 startup error 2 37
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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.

762 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