-1

I want to convert the CSV with double quotes into OpenCSV (no double quotes and comma is escaped with backslash) using unix utilities SED or AWK. I do find examples with perl or java online, but i am looking for one which is simply done using basic SED or AWK.

Arun Annamalai
  • 785
  • 1
  • 7
  • 20
  • 1
    Example input/output for those of us who don't know OpenCSV standards? – Sam Aug 27 '15 at 18:23
  • Which part of the task is causing you difficulty? – Steven Doggart Aug 27 '15 at 18:29
  • OpenCSV: http://opencsv.sourceforge.net/, https://code.google.com/p/opencsv/ (insert warnings about Sourceforge and Google Code). – Keith Thompson Aug 27 '15 at 18:52
  • Are we talking about the same opencsv that can be found at [sourceforge](https://sourceforge.net/projects/opencsv/) ? Or is this some CSV standard - in which case could you please provide a link to the standards because when I google opencsv I just get the open source project. The OpenCSV parser handles double quotes and commas without issue. – Scott Conway Aug 27 '15 at 18:53
  • 1
    You don't, at least not easily. Use a CSV parser. – chepner Aug 27 '15 at 18:53

2 Answers2

0

Not sure about OpenCSV standards, but going by your description you can use this to do a find and replace using SED.

sed -i -e 's/FINDME/REPLACEWITH/g' folder/file.csv

Multiple find/replace can be separated by a semi-colon ;. -i edits a file in place and -e runs a script.

So for your particular example, backslashes and commas make it a little difficult, but this should work:

sed -i -e 's/"/'\''/g;s/,/\\,/g' file.csv

Mr Office
  • 290
  • 1
  • 9
0

From your description, this might be what you are after:

awk -F'" *, *"|^ *"|" *$' '{a="";for(i=2;i<=NF-1;i++){gsub(/,/,"\\,",$i); if(a){a=a","$i}else{a=$i}};print a}

A bash example:

awk -F'" *, *"|^ *"|" *$' '{a="";for(i=2;i<=NF-1;i++){gsub(/,/,"\\,",$i); if(a){a=a","$i}else{a=$i}};print a}'<<<$'"a","b",","\n"d", "e" ,",,,"'
a,b,\,
d,e,\,\,\,
estani
  • 24,254
  • 2
  • 93
  • 76