I am unable to replace double backslash followed by quote \\'
in sed. This is my current command
echo file.txt | sed "s:\\\\':\\':g"
The above command not only replaces \\'
with \'
it also replaces \'
with '
How could I just replace exact match?
Input:
'one', 'two \\'change', 'three \'unchanged'
Expected:
'one', 'two \'change', 'three \'unchanged'
Actual:
'one', 'two \'change', 'three 'unchanged'
Here's a couple of examples where using double quotes makes perfectly sense: $ echo "We're good!" | sed "s/'re/ are/" We are good! $ name="John"; echo "Hello, I'm <name>." | sed "s/<name>/$name/" Hello, I'm John. Save this answer.
A single-line sed command can remove quotes from the start and end of the string. The above sed command executes two expressions against the variable value. The first expression 's/^"//' will remove the starting quote from the string. Second expression 's/"$//' will remove the ending quote from the string.
$ sed "s/\\\\\\\'/\\\'/g" file
'one', 'two \'change', 'three \'unchanged'
Here is a discussion on why sed needs 3 backslashes for one
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With