Solved

java split csv file into multiple csv files

Posted on 2014-12-24
7
332 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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.…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 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

10 Experts available now in Live!

Get 1:1 Help Now