I’m looking for something that will translate a string as follows, using only bash / standard Linux commands:
- Single-quotes surrounding a string should be removed
- Double-quotes surrounding a string should be removed
- Unquoted strings should remain the same
- Strings with unmatched surrounding quotes should remain the same
- Single-quotes that don’t surround the string should remain
- Double-quotes that don’t surround the string should remain
For example:
- ‘Food’ should become Food
- “Food” should become Food
- Food should remain the same
- ‘Food” should remain the same
- “Food’ should remain the same
- ‘Fo’od’ should become Fo’od
- “Fo’od” should become Fo’od
- Fo’od should remain the same
- ‘Fo”od’ should become Fo”od
- “Fo”od” should become Fo”od
- Fo”od should remain the same
Thank you!
Advertisement
Answer
This should do it:
sed "s/^(["'])(.*)1$/2/g" in.txt
Where in.txt is:
"Fo'od' 'Food' "Food" "Fo"od' Food 'Food" "Food' 'Fo'od' "Fo'od" Fo'od 'Fo"od' "Fo"od" Fo"od
And expected.txt is:
"Fo'od' Food Food "Fo"od' Food 'Food" "Food' Fo'od Fo'od Fo'od Fo"od Fo"od Fo"od
You can check they match with:
diff -s <(sed "s/^(["'])(.*)1$/2/g" in.txt) expected.txt