Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??


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.


Input :


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,

In reply to replace/substituion 4th field by hyans.milis

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (6)
    As of 2015-04-01 00:40 GMT
    Find Nodes?
      Voting Booth?

      When putting a smiley right before a closing parenthesis, do you:

      Results (674 votes), past polls