For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2007 > Splitting a CSV file at a variable number









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Splitting a CSV file at a variable number
Sum_duud

2007-06-29, 9:58 pm

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.

Chas Owens

2007-06-29, 9:58 pm

On 6/28/07, sum_duud <Turnbull.Glenn@gmail.com> wrote:
> 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.

snip

use an array of file handles (warning, untested):

use strict;
use warnings;

open my $in, '<', "phase.csv" or die "Could not open phase.csv: $!\n";
<$in>; #discard first line

my @file = map {
open my $fh, '>', "phase_$_.csv"
or die "could not open phase_$_.csv: $!";
$fh
} 0 .. 20;

while (<$in> ) {
chomp;
my ($x, $y, $z, $phase) =split /\s*,\s*/;
my $file = $file[$phase];
print $file "$x, $y, $z, $phase\n";
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com