Splitting a CSV file at a variable number
- From: Turnbull.Glenn@xxxxxxxxx (Sum_duud)
- Date: Wed, 27 Jun 2007 22:20:26 -0700
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.
.
- Follow-Ups:
- Re: Splitting a CSV file at a variable number
- From: Chas Owens
- Re: Splitting a CSV file at a variable number
- Prev by Date: Re: String Manipulation
- Next by Date: Re: DBI, postgresql and large table
- Previous by thread: DBI, postgresql and large table
- Next by thread: Re: Splitting a CSV file at a variable number
- Index(es):
Relevant Pages
|