Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a double backslash followed by quote (\\') using sed?

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'
like image 919
Jardalu Avatar asked Jun 14 '15 05:06

Jardalu


People also ask

How do you use double quotes in sed?

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.

How do you escape a double quote in sed?

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.


1 Answers

$ sed "s/\\\\\\\'/\\\'/g" file
'one', 'two \'change', 'three \'unchanged'

Here is a discussion on why sed needs 3 backslashes for one

like image 142
nu11p01n73R Avatar answered Nov 15 '22 08:11

nu11p01n73R