hyans.milis has asked for the wisdom of the Perl Monks concerning the following question:
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.
thanksInput :
File1.txt
Code :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,
Output :#!/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>
Expected result :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>
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,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: replace/substituion 4th field
by tmharish (Friar) on Feb 20, 2013 at 08:15 UTC | |
by jwkrahn (Abbot) on Feb 20, 2013 at 17:04 UTC | |
by tmharish (Friar) on Feb 21, 2013 at 06:39 UTC | |
|
Re: replace/substituion 4th field
by põhjapõder (Novice) on Feb 20, 2013 at 08:21 UTC | |
|
Re: replace/substituion 4th field
by 2teez (Vicar) on Feb 20, 2013 at 08:12 UTC | |
by scsijon (Initiate) on Feb 20, 2013 at 21:08 UTC | |
|
Re: replace/substituion 4th field
by BillKSmith (Monsignor) on Feb 20, 2013 at 13:58 UTC | |
|
Re: replace/substituion 4th field
by 7stud (Deacon) on Feb 20, 2013 at 18:10 UTC | |
by Anonymous Monk on Feb 20, 2013 at 21:40 UTC | |
by 7stud (Deacon) on Feb 21, 2013 at 07:00 UTC | |
|
Re: replace/substituion 4th field
by hyans.milis (Novice) on Mar 02, 2013 at 12:19 UTC |