28.05.2012

sed and strong quotes '

If you want to substitute strings via sed, it is very easy:
# echo 'hello world!' |sed 's/hello/it is a cool/'
it is a cool world!
But what if you want to use a strong quote inside the substitution?
# echo 'hello world!' |sed 's/hello/it's a cool/'
gets interpreted from the shell and nothing happens. Next try: Escape the strong quote with a backslash:
 # echo 'hello world!' |sed 's/hello/it\'s a cool/'
Does not work...
Any other ideas? What abaout using the dollar (money always helps ;-):
 # echo 'hello world!' |sed $'s/hello/it\'s a cool/'
it's a cool world!

Kommentare:

  1. what about:

    echo 'hello world!' |sed "s/hello/it's a cool/"

    AntwortenLöschen
  2. $echo 'hello world'|sed "s/hello/it's a cool/"
    it's a cool world
    $
    ;-)

    AntwortenLöschen
  3. cool, didn't know that money also helps here !

    $ echo 'hello world!' |sed "s/hello/it's a cool/"
    it's a cool world!

    AntwortenLöschen