How to escape single quote in sed?

SedEscapingQuote

Sed Problem Overview


How to escape a single quote in a sed expression that is already surrounded by quotes?

For example:

sed 's/ones/one's/' <<< 'ones thing'

Sed Solutions


Solution 1 - Sed

Quote sed codes with double quotes:

    $ sed "s/ones/one's/"<<<"ones thing"   
    one's thing

I don't like escaping codes with hundreds of backslashes – hurts my eyes. Usually I do in this way:

    $ sed 's/ones/one\x27s/'<<<"ones thing"
    one's thing

Solution 2 - Sed

One trick is to use shell string concatenation of adjacent strings and escape the embedded quote using shell escaping:

sed 's/ones/two'\''s/' <<< 'ones thing'

two's thing

There are 3 strings in the sed expression, which the shell then stitches together:

sed 's/ones/two'

\'

's/'

Hope that helps someone else!

Solution 3 - Sed

The best way is to use $'some string with \' quotes \''

eg:

sed $'s/ones/two\'s/' <<< 'ones thing'

Solution 4 - Sed

Just use double quotes on the outside of the sed command.

$ sed "s/ones/one's/" <<< 'ones thing'
one's thing

It works with files too.

$ echo 'ones thing' > testfile
$ sed -i "s/ones/one's/" testfile
$ cat testfile
one's thing

If you have single and double quotes inside the string, that's ok too. Just escape the double quotes.

For example, this file contains a string with both single and double quotes. I'll use sed to add a single quote and remove some double quotes.

$ cat testfile
"it's more than ones thing"
$ sed -i "s/\"it's more than ones thing\"/it's more than one's thing/" testfile 
$ cat testfile 
it's more than one's thing

Solution 5 - Sed

Escaping single quote in [tag:sed]: 3 different ways:

From fragile to solid...

1. Using double-quotes to enclose sed script:

Simpliest way:

sed "s/ones/one's/" <<< 'ones thing'

But using double-quote lead to shell variables expansion and backslashes to be considered as shell escape before running sed.

1.1. Specific case without space and special chars

In this specific case, you could avoid enclosing at shell level (command line):

sed s/ones/one\'s/ <<<'ones thing'

will work until whole sedscript don't contain spaces, semicolons, special characters and so on... (fragile!)

2. Using octal or hexadecimal representation:

This way is simple and efficient, if not as readable as next one.

sed 's/ones/one\o047s/' <<< 'ones thing'

sed 's/ones/one\x27s/' <<< 'ones thing'

And as following character (s) is not a digit, you coul write octal with only 2 digits:

sed 's/ones/one\o47s/' <<< 'ones thing'

3. Creating a dedicated sed script

First ensure correct path to sed tool:

which sed
/bin/sed

Then adapt shebang in following:

cat <<"eosedscript" >sampleSedWithQuotes.sed
#!/bin/sed -f

s/ones/one's/;
eosedscript
chmod +x sampleSedWithQuotes.sed

From there, you could run:

./sampleSedWithQuotes.sed <<<'ones thing'
one's thing

This is the strongest and simpliest solution as your script is the most readable:
$ cat sampleSedWithQuotes.sed

#!/bin/sed -f

s/ones/one's/;
3.1 You coud use -i sed flag:

As this script use sed in shebang, you could use sed flags on command line. For editing file.txt in place, with the -i flag:

echo >file.txt 'ones thing'
./sampleSedWithQuotes.sed -i file.txt
cat file.txt
one's thing

Solution 6 - Sed

This is kind of absurd but I couldn't get \' in sed 's/ones/one\'s/' to work. I was looking this up to make a shell script that will automatically add import 'hammerjs'; to my src/main.ts file with Angular.

What I did get to work is this:

apost=\'
sed -i '' '/environments/a\
import '$apost'hammerjs'$apost';' src/main.ts

So for the example above, it would be:

apost=\'
sed 's/ones/one'$apost's/'

I have no idea why \' wouldn't work by itself, but there it is.

Solution 7 - Sed

Some escapes on AppleMacOSX terminals fail so:

sed 's|ones|one'$(echo -e "\x27")'s|1' <<<'ones thing'

Solution 8 - Sed

I know this is going to sound like a cop out but I could never get sed working when there were both single and double quotes in the string. To help any newbies like me that are having trouble, one option is to split up the string. I had to replace code in over 100 index.hmtl files. The strings had both single and double quotes so I just split up the string and replaced the first block with <!-- and the second block with -->. It made a mess of my index.html files but it worked.

Solution 9 - Sed

use an alternative string seperator like ":" to avoid confusion with different slashes

sed "s:ones:one's:" <<< 'ones thing'

or if you wish to highligh the single quote

sed "s:ones:one\'s:" <<< 'ones thing'

both return

one's thing

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThomas BrattView Question on Stackoverflow
Solution 1 - SedKentView Answer on Stackoverflow
Solution 2 - SedThomas BrattView Answer on Stackoverflow
Solution 3 - SedJohn CView Answer on Stackoverflow
Solution 4 - SedStef ForresterView Answer on Stackoverflow
Solution 5 - SedF. HauriView Answer on Stackoverflow
Solution 6 - SedAmbrownView Answer on Stackoverflow
Solution 7 - SedPaSeView Answer on Stackoverflow
Solution 8 - SedmcmacersonView Answer on Stackoverflow
Solution 9 - SedbrainelectronicsView Answer on Stackoverflow