bguiz
bguiz

Reputation: 28587

sed replace ' with \'

This:

echo "Hel'lo" | sed s/\'/\\\'/g

Yields

Hel'lo

What I want is this:

Hel\'lo

What am I missing?

Upvotes: 4

Views: 35055

Answers (4)

ghoti
ghoti

Reputation: 46846

You can also do it with all single quotes:

echo "Hel'lo" | sed 's/'\''/\\'\''/g'

And since your question is also tagged bash, I might as well point out that you don't even need sed:

[ghoti@pc ~]$ foo="Hel'lo"
[ghoti@pc ~]$ echo "${foo/\'/\'}"
Hel\'lo

Upvotes: 1

potong
potong

Reputation: 58420

This might work for you:

echo "Hel'lo" | sed 's/'\''/\\&/'

Upvotes: 0

Adrian Pronk
Adrian Pronk

Reputation: 13906

Or without quoting the sed argument:

echo "Hel'lo" | sed s/\'/\\\\\'/g

Upvotes: 1

Seth Robertson
Seth Robertson

Reputation: 31451

echo "Hel'lo" | sed "s/'/\\\'/g"
Hel\'lo

Also

echo "Hel'lo" |  sed s/\'/\\\\\'/g

Upvotes: 14

Related Questions