Sed/awk to remove double quotes ” that are inside of curly braces {}

awkcsvdeletesed

I have a csv file with data similar to this:

"1b579a5e-9701-40eb-bd36-2bc65169da99","week14_Friday-019","6907eaad-1aff-4d26-9088-ba20374b67c0","2181-019","f20af5bb-c716-42e0-9b9d-cbolf5bfecea","15-BIO-2001","COLLEGE Bio 1","d39330be-df56-4365-8fb4-37e68d040c52","Which engine has the smaller efficiency?","{choices:[a","b","c","d],"type:MultipleChoice}","{solution:[0],"selectAll:false,{"selectMultiple:false",}"type:MultipleChoice}","2016-04-25 00:30:19.000","1922ac5a-6ff6-4ea4-9078-6df4d85d294f","{solution:[0],"type:MultipleChoice}","1","1116911f-8ee5-45c3-b173-a6be681bb15a","FakeLastName","FakeFirstName","FakeName@mail.edu","Student"

I want to remove the double-quotes ", but only if they are inside of the curly braces {}, preferably with sed or awk. Desired output is:

"1b579a5e-9701-40eb-bd36-2bc65169da99","week14_Friday-019","6907eaad-1aff-4d26-9088-ba20374b67c0","2181-019","f20af5bb-c716-42e0-9b9d-cbolf5bfecea","15-BIO-2001","COLLEGE Bio 1","d39330be-df56-4365-8fb4-37e68d040c52","Which engine has the smaller efficiency?","{choices:[a,b,c,d],type:MultipleChoice}","{solution:[0],selectAll:false,{selectMultiple:false,}type:MultipleChoice}","2016-04-25 00:30:19.000","1922ac5a-6ff6-4ea4-9078-6df4d85d294f","{solution:[0],type:MultipleChoice}","1","1116911f-8ee5-45c3-b173-a6be681bb15a","FakeLastName","FakeFirstName","FakeName@mail.edu","Student"

Any help would be greatly appreciated. Thanks!

Best Answer

It would be easier with sed:

sed -e :1 -e 's/\({[^}]*\)"\([^}]*}\)/\1\2/g; t1'

Or perl:

perl -pe 's{\{.*?\}}{$& =~ s/"//gr}ge'

Note that it assumes there's no nested {...}.

To handle nested {...}, you can use perl's recursive regexp capabilities:

perl -pe 's(\{(?:[^{}]++|(?0))*\})($& =~ s/"//gr)ge'

With sed, working our way outwards to escape the inner {...}s before removing the "s:

sed 's/_/_u/g
     :1
     s/\({[^{}]*\){\([^{}]*\)}/\1_<\2_>/g; t1
     :2
     s/\({[^}]*\)"\([^}]*}\)/\1\2/g; t2
     s/_</{/g; s/_>/}/g;s/_u/_/g'
Related Question