http://www.perlmonks.org?node_id=1160556

BigRedEO has asked for the wisdom of the Perl Monks concerning the following question:

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; }