awksed

Removing first and last double quotes (preferably sed/awk)


I only want to remove the first and last double quotes

Input:

word1 -word2 {word3} -word4 {"word5 'word6' "word7 word8" .word9"} -word10 (word11)
word1 -word2 {word3} -word4 {"word5 'word6' word7 word8 .word9"} -word10 (word11)
word1 -word2 {word3} -word4 {"word5 'word6' "word7 (word8)" .word9"} -word10 (word11)

Expected output:

word1 -word2 {word3} -word4 {word5 'word6' "word7 word8" .word9} -word10 (word11)
word1 -word2 {word3} -word4 {word5 'word6' word7 word8 .word9} -word10 (word11)
word1 -word2 {word3} -word4 {word5 'word6' "word7 (word8)" .word9} -word10 (word11)

Tried this to remove the first " but it did not work:

sed 's/"\({\)/\1/g' inputfile > outputfile

Removing the first double quote found in the line, that is after a "{" and before a "}" can be accepted.

Assuming word can be of different lengths too.


Solution

  • You may use this sed with a capture group:

    sed -E 's/\{"(.*)"}/{\1}/' file
    
    word1 -word2 {word3} -word4 {word5 'word6' "word7 word8" .word9} -word10 (word11)
    word1 -word2 {word3} -word4 {word5 'word6' word7 word8 .word9} -word10 (word11)
    word1 -word2 {word3} -word4 {word5 'word6' "word7 (word8)" .word9} -word10 (word11)
    

    Here pattern {"(.*)"} matches longest match between {" and "} and captures test in the middle into a capture group.