Splitting a CSV file at a variable number



I have a large CSV file of the format x,y,z,number (a coordinate file)
that I need to parse and output as individual files based on the
fourth field (column 3). The file looks like the sample below, and
the fourth field can be from zero to 20,
so in essence I would like the perl script to output all the fourth
column "0" values to a file called phase_0.csv and all the "1" values
to phase_1.csv etc.

257673.711,6275192.977,150.500,0
257693.711,6275167.977,147.500,0
257713.711,6275167.977,147.500,2
257693.711,6275192.977,147.500,0
257713.711,6275192.977,147.500,0
257693.711,6275167.977,150.500,2
257713.711,6275167.977,150.500,2
257693.711,6275192.977,150.500,0
257713.711,6275192.977,150.500,2
257733.711,6275167.977,147.500,2
257753.711,6275167.977,147.500,2
257733.711,6275192.977,147.500,2

The perl script I have will work on one file, and one defined field,
but is there a way or telling perl to increment the search of the
fourth field value and effectively looping through the data until
there are no more fourth field values. ?.
The script I have is below:-


open( DEFAULT , "phase.csv") or die "Could not open $input.csv\n";
$blank = <DEFAULT>;

############################################################
# OPEN OUTPUT FILE AND WRITE HEADER LINE
############################################################

open( OUT , ">phase_1.csv") or die "could not open $output\n";
print OUT "X, Y, Z, Phase\n";

# Read the data from csv file
# -----------------------------------------

while (my $line = <DEFAULT>)
{
chomp $line;
my @col =split (/\s*,\s*/ , $line);
$phase = $col[3];
if ($phase == 1)
{
print OUT "$col[0], $col[1], $col[2], $col[3] \n";
}
}
close DEFAULT;
close OUT;
exit 0;
# pause so that window does not close
print "\nPress <Enter> \n";
<STDIN>;


Any help would be greatly appreciated.

.



Relevant Pages

  • [OT] Bash Script Help
    ... I am writing a shell script to parse a CSV file to prepare it for ... the CSV file looks like this: ... Can I use command substitution in a sed substitute command? ...
    (Debian-User)
  • ldif to CSV
    ... in CSV file. ... I have to write a perl script that takes the fields that correspond to the ... Lets say the Outlook CSV file has the first and second field which does not ... two empty spaces seperated by comma or semicolon. ...
    (perl.beginners)
  • Re: How to import a tab delimited text file into MS Access database
    ... If you file has any indication of which data goes in which field you could ... just parse the CSV file and update the records one by one to the .MDB file ... The table is created and the table structure is defined using DAO ...
    (microsoft.public.vc.mfc)
  • Re: Simple XML-to-Python conversion
    ... > If I used a simple INI file or CSV file, I would simply have to parse ... > parse a document once and then re-parse your information into a format ... A configuration file is only ...
    (comp.lang.python)
  • Unrecoverable error 332 string/array memory overflow
    ... During a .csv import from a fairly big .csv file, and ... place the fields in that cLine into an array and then AADDing that ... in a variety of places during the 'parse' loop ... To forestall this crash I put in a delay of 4 seconds after reaching ...
    (comp.lang.clipper)