Change a csv file

By | September 2, 2009

If you want to use a huge csv file, an easy way to do this is to parse it and write another file. Bellow is an example, which takes the values from old_file.csv, do some changes and write another file new_file.csv

#! /usr/bin/perl
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year=1900+$yearOffset;
$time=”$year-$month-$dayOfMonth-$hour$minute$second”;
print “Started at $timenn”;
open (MYFILE, “>>”, ‘parse_logs.txt’);
print MYFILE “Started at $timen”;
close (MYFILE);
$i=0;
open (FINAL, “>”, ‘new_file.csv’);
open (FILE, “<“, ‘old_file.csv’);
@lines=<FILE>;
foreach $l(@lines){
$i++;
print “$i n”;
@values=split(‘,’,$l);
$values[1]=$i; #asign a new ID
$values[2]=~s/s*//g; # remove all the whitespace
#other changes
$l=join(‘,’,@values);
print FINAL “$l”;
}
close (FILE);
close (FINAL);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year=1900+$yearOffset;
$time=”$year-$month-$dayOfMonth-$hour$minute$second”;
print “Finished at $timenn”;
open (MYFILE, “>>”, ‘parse_logs.txt’);
print MYFILE “Started at $timen”;
close (MYFILE);
#! /usr/bin/perl
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year=1900+$yearOffset;
$time=”$year-$month-$dayOfMonth-$hour$minute$second”;
print “Started at $timenn”;
open (MYFILE, “>>”, ‘parse_logs.txt’);
print MYFILE “Started at $timen”;
close (MYFILE);
$i=0;
open (FINAL, “>”, ‘new_file.csv’);
open (FILE, “<“, ‘old_file.csv’);
@lines=<FILE>;
foreach $l(@lines){
$i++;
print “$i n”;
@values=split(‘,’,$l);
$values[1]=$i; #asign a new ID
$values[2]=~s/s*//g; # remove all the whitespace
#other changes
$l=join(‘,’,@values);
print FINAL “$l”;
}
close (FILE);
close (FINAL);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year=1900+$yearOffset;
$time=”$year-$month-$dayOfMonth-$hour$minute$second”;
print “Finished at $timenn”;
open (MYFILE, “>>”, ‘parse_logs.txt’);
print MYFILE “Started at $timen”;
close (MYFILE);

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.