Hi,
i'm newbie in Perl and facing the problem when creating script which replacing the 4th field from csv file.
after running the script, the result is not same with my expectation.
need your help regarding substitution of 4th field.
actually i will use the script in production with ~20Mil records on each file.
please advice also whether any faster method to process huge data in one file without importing to the DB.
thanks
Input :
File1.txt
623192729079,510993192729079,19,322,0,133,282051608,
623192728769,510993192728769,19,310,0,118,84950715,
623192729901,510993192729901,19,70,0,
623192609007,510993192609007,19,22,0,
623416771429,510993416771429,19,70,0,
622319309157,510992319309157,19,22,0,
623192724581,510993192724581,19,70,0,
622319381619,510992319381619,19,70,0,
622198575655,510992198575655,19,1,0,
623192724589,510993192724589,19,70,0,
622743581281,510992743581281,19,71,0,
Code :
#!/usr/bin/perl
use strict;
use warnings;
my $file = $ARGV[0] or die "Need to get CSV file on the command line\n
+";
my $sum = 0;
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
while (my $line = <$data>) {
chomp $line;
my @fields = split "," , $line;
my $sum = $fields[3];
if ($sum > 310) {
my $sum2= "volemd";
chomp($sum2);
$line =~ s/(.*)\,(.*)\,(.*)\,(.*)\,(.*)\,(.*)/$1\,$sum2\,$3\,$4,$5
+,$6/g;
} elsif ($sum == 70){
my $sum3= "volemd1";
chomp($sum3);
$line =~ s/(.*)\,(.*)\,(.*)\,(.*)\,(.*)\,(.*)/$1\,$sum3\,$3\,$4,$5
+,$6/g;
}
;
print "$line\n";
}
</p>
Output :
623192729079,510993192729079,19,volemd,0,133,282051608,
623192728769,510993192728769,19,310,0,118,84950715,
623192729901,volemd1,19,70,0,
623192609007,510993192609007,19,22,0,
623416771429,volemd1,19,70,0,
622319309157,510992319309157,19,22,0,
623192724581,volemd1,19,70,0,
622319381619,volemd1,19,70,0,
622198575655,510992198575655,19,1,0,
623192724589,volemd1,19,70,0,
622743581281,510992743581281,19,71,0,
</p>
Expected result :
623192729079,510993192729079,19,volemd,0,133,282051608,
623192728769,510993192728769,19,310,0,118,84950715,
623192729901,510993192729901,19,volemd1,0,
623192609007,510993192609007,19,22,0,
623416771429,510993416771429,19,volemd1,0,
622319309157,510992319309157,19,22,0,
623192724581,510993192724581,19,volemd1,0,
622319381619,510992319381619,19,volemd1,0,
622198575655,510992198575655,19,1,0,
623192724589,510993192724589,19,volemd1,0,
622743581281,510992743581281,19,71,0,