need to divide a 30,000 line comma delimitated flat file to 1,000 lines/file

MrHappyMonkey

Diamond Member
Mar 15, 2001
3,091
0
0
Hello,

I've got a flat file that is comma delimitated. There are aproximately 31,000 records in this file. I need to divide this single file into 31 seperate flat files consisting of 1,000 records. Is there an app that'll do this? I've got the flat file imported into MS Excel.

Is there an app or script that can do this for me?
 

kt

Diamond Member
Apr 1, 2000
6,032
1,348
136
You can write a short Perl script to do this. Don't ask me how as I haven't touched Perl for ages.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
perl:

$file = 0;
$count = -1;
open IN, "access_log";
while( <IN> ){
$temp = $_;
$count++;
if($count % 1000 == 0){
close OUT;
$file++;
open OUT, ">$file.txt";
}
print OUT $temp;
}

close OUT;
close IN;


edit: been writng too much java lately, it's starting to show up in my perl. Should run ok now :)
 

MrHappyMonkey

Diamond Member
Mar 15, 2001
3,091
0
0
Originally posted by: notfred
perl: $file = 0; $count = 0; open IN, "file.txt"; while( <IN>){ $temp = $_; $count++; if(count%1000 == 0){ close OUT; file++; open OUT ">$file.txt"; } print OUT $temp; } close OUT; close IN;

do I just save that has a *.pl file and upload it and "file.txt" to my web server and execute it? or is there a way to run it through windows (i dont have apache installed)
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: MrHappyMonkey
Originally posted by: notfred
perl: $file = 0; $count = 0; open IN, "file.txt"; while( <IN>){ $temp = $_; $count++; if(count%1000 == 0){ close OUT; file++; open OUT ">$file.txt"; } print OUT $temp; } close OUT; close IN;

do I just save that has a *.pl file and upload it and "file.txt" to my web server and execute it? or is there a way to run it through windows (i dont have apache installed)

you dont need a webserver just a perl interpertor.
 

MrHappyMonkey

Diamond Member
Mar 15, 2001
3,091
0
0
Originally posted by: Ameesh
Originally posted by: MrHappyMonkey
Originally posted by: notfred perl: $file = 0; $count = 0; open IN, "file.txt"; while( <IN>){ $temp = $_; $count++; if(count%1000 == 0){ close OUT; file++; open OUT ">$file.txt"; } print OUT $temp; } close OUT; close IN;
do I just save that has a *.pl file and upload it and "file.txt" to my web server and execute it? or is there a way to run it through windows (i dont have apache installed)
you dont need a webserver just a perl interpertor.

where can i get one that will run on winXP?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
ActivePerl for windows.

If you install that, you can jsut save the above text as "somefile.pl" and put "file.txt" (whre file.txt is your flatfile) in the same directory, and then double click the perl script. It'll make files numbered 1.txt, 2.txt, 3.txt, etc for each 1000 lines of your flat file.
 

pac1085

Diamond Member
Jun 27, 2000
3,456
0
76
Originally posted by: MrHappyMonkey
Originally posted by: Ameesh
Originally posted by: MrHappyMonkey
Originally posted by: notfred perl: $file = 0; $count = 0; open IN, "file.txt"; while( <IN>){ $temp = $_; $count++; if(count%1000 == 0){ close OUT; file++; open OUT ">$file.txt"; } print OUT $temp; } close OUT; close IN;
do I just save that has a *.pl file and upload it and "file.txt" to my web server and execute it? or is there a way to run it through windows (i dont have apache installed)
you dont need a webserver just a perl interpertor.

where can i get one that will run on winXP?

CPAN