1

We have example of partial CSV file ( with only 3 fields )

our target is to remove all " characters but not inside the double quotes

ssl-server,"ssl.server.truststore.type","jks"
tez-env,"enable_heap_"\n"dump","false"
tez-env,"heap_dump_location"\n"port","/tmp"
tez-env,"tez_user","tez"

expected output:

ssl-server,ssl.server.truststore.type,jks
tez-env,enable_heap_"\n"dump,false
tez-env,heap_dump_location"\n"port,/tmp
tez-env,tez_user,tez
1

I would recommend to use dedicated software for csv manipulation, although this case could be solved with simple sed if there are no commas inside double quotes:

sed 's/^"//;s/",/,/g;s/,"/,/g;s/"$//' file
  • The CVSKit toolkit doesn't properly grok this data since it's improperly quoted from the start. – Kusalananda Aug 22 '17 at 17:36
1

Short sed solution:

sed 's/^"\|"$//g; s/"*,"*/,/g;' file

Or awk approach:

awk -F, '{ for(i=1;i<=NF;i++) gsub(/^"|"$/,"",$i) }1' OFS=',' file

The output (for both approaches):

ssl-server,ssl.server.truststore.type,jks
tez-env,enable_heap_"\n"dump,false
tez-env,heap_dump_location"\n"port,/tmp
tez-env,tez_user,tez

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.