I've asked this question elsewhere, this after have tried many things I've found searching Google, but have not had luck yet. I will try here (hopefully I'll be much better at posting my question after having so many problems the first time I tried to post a question here earlier this week. I'm such a total noob) -

I have happened upon a problem with a program I'm writing that parses through a CSV file with a few million records: two fields in each record have comments that users have input, and sometimes they use commas within their comments. If there are commas input, that field will be contained in double quotes in the record. I need to replace any commas found in those fields with a space. Here is one such record from the file to give you an idea. -

1925,47365,2,650187016,1,1,"MADE FOR DRAWDOWNS, NEVER P/U",16,IFC 8112NP,Standalone-6,,,44,10/22/2015,91607,,B24W02651,,"PA-3, PURE",4/28/2015,1,0,,1,MAN,,CUST,,CUSTOM MATCH,0,TRUE,TRUE,O,C48A0D001EF449E3AB97F0B98C811B1B,POS.MISTINT.V0000.UP.Q,PROD_SMISA_BK,414D512050524F445F504F5331393235906F28561D2F0020,10/22/2015 9:29,10/22/2015 9:30

NOTE - I do not have the Text::CSV module available to me, nor will it be made available in the server I am using. Here is part of my code in parsing this file. The first thing I do is concatenate the very first three fields and prepend that concatenated field to each line. Then I want to clear out the commas in @fields7,19 (the thing I'm having trouble figuring out), then format the DATE in three fields and the DATETIME in two fields. The only part I can't figure out is replacing those commas with a space. Is there a tr/,/ /; line or regex line or anything else that would work? -

my @data; # Read the lines one by one. while ( $line = <$FH> ) { # split the fields, concatenate the first three fields, # and add it to the beginning of each line in the file chomp($line); my @fields = split(/,/, $line); unshift @fields, join '_', @fields[0..2]; # remove user input commas in fields 7 and 19 $_ = for fields[7,19]; # format DATE and DATETIME fields for MySQL/sqlbatch60 $_ = join '-', (split /\//)[2,0,1] for @fields[14,20,23]; $_ = Time::Piece->strptime($_,'%m/%d/%Y %H:%M')->strftime('%Y- +%m-%d %H:%M') for @fields[38,39]; # write the parsed record back to the file push @data, \@fields; }


In reply to Replace commas with spaces between quotes, parsing CSV by BigRedEO

Title:
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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.