Convert CSV file (with double quoted strings) to pipe delimited file

Hi, could some help me convert CSV file (with double quoted strings) to pipe delimited file:

here you go with the same data:

1,Friends,"$3.99 per 1,000 listings",8158

here " 1,000 listings " should be a single field.

Thanks,
Ram

Expecting this .. ??

$ sed 's,\,,|,g' inputfile
1|Friends|"$3.99 per 1|000 listings"|8158

I'm expecting like this:

1|Friends|$3.99 per 1,000 listings|8158

I would also do the same based on his/her requirement. but the output looks.... strange :smiley:

With GNU awk 4:

awk '{
  for (i = 0; ++i <= NF;) {
    if (substr($i, 1, 1) == "\"") {
      len = length($i)
      $i = substr($i, 2, len - 2)
      }
    printf "%s", ($i (i < NF ? OFS : RS))  
    }
  }' OFS=\| FPAT='([^,]+)|("[^"]+")' infile

With Perl:

perl -MText::ParseWords -nle'
  print join "|", parse_line(",",0, $_);
  ' infile 

this should work 4 u:

awk -F'"' '{gsub(/,/,"|",$1);gsub(/,/,"|",$3);} 1' yourFile 

example output:

kent$  echo '1,Friends,"$3.99 per 1,000 listings",8158'|awk -F'"' '{gsub(/,/,"|",$1);gsub(/,/,"|",$3);} 1' 

1|Friends| $3.99 per 1,000 listings |8158

it just errors:

awk: illegal statement near line 1
awk: bailing out near line 1

which system and awk do you have?

I tested that line under:

kent$  uname -ro     
2.6.28-19-generic GNU/Linux

kent$  awk --version
GNU Awk 3.1.6

Thank you, it works with nawk, not sure why,