5

This works fine:

sed -i 's#    @driver.find_element(:xpath, "//a\[contains(@href,##' temp_spec.rb

against a source of

@driver.find_element(:xpath, "//a[contains(@href,'change_district')]").click

I am just left with:

'change_district')]").click`

but when I try to add a single quote at the end it fails:

sed -i 's#    @driver.find_element(:xpath, "//a\[contains(@href,\'##' temp_spec.rb

syntax error near unexpected token `('

I am using \' to escape the single quote.

Note that I am using a # as a delimiter instead of the normal / delimiter as there are /'s in the text.

wie5Ooma
  • 450

2 Answers2

13

The strings inside single quotes are used verbatim by the shell (and hence cannot contain other single quote, since that would be treated as the closing one of a pair). That said, you have several options:

  • end the single-quoted string, add escaped (either by backslash or double quotes) single quote, and immediately start the following part of your string:

    $ echo 'foo'\''bar' 'foo'"'"'bar'
    foo'bar foo'bar
    
  • use double quotes for your string:

    $ echo "foo'bar"
    foo'bar
    

    this of course means that you have to be careful about what gets expanded inside the double quotes by your shell;

  • if you are using utilities that understand \xHH or \0nnn escaping (GNU sed does), you can use one of those (ASCII single quote is at code point 39 (0x27), hence e.g. \x27).

peterph
  • 30,838
1

That syntax error there seems to be hinting at unescaped regex. I personally don't use sed that often, but if I remember correctly, you'll also need to escape things like parenthesis (and with double backslashes if you are using double quotes). Here is a list of possible characters that you may need to escape...