-2

I`m trying to replace a html from several files using sed without sucess. I need to replace this:

<link rel='shortcut icon' href='/admin/img/favicons/favicon.ico' type='image/x-icon'>

with this:

<link rel="icon" href="/admin/img/logo.svg">

This is the last command i tried, but always gives errors:

#find . -type f -name "*.php" -exec sed -i'' -e 's/<link rel='shortcut\ icon' href='\/admin\/img\/favicons\/favicon.ico' type='image/x-icon'>/<link rel\="icon" href\="admin/img/logo.svg">/g' {} +
sed: -e expression #1, char 42: unknown option to `s'

can you please help?

  • 1
  • "always gives errors" is as useless as "it doesn't work". Add the actual error to your question. 2. Your search text contains single-quotes. You can't embed single-quotes inside single quotes, and you can't escape them with backslash either. Replace every instance of ' inside your sed script with '\'' (end-quote, escaped quote, start-quote). Or wrap the sed script in double-quotes instead of single-quotes, and escape the double-quotes in the replacement text with \"...unlike single-quotes, you can escape double-quotes.
  • – cas Nov 19 '21 at 01:46
  • BTW, you can escape single-quotes with a backslash if you use $' ... ' to quote the sed script instead of just ' ... '. This may have other side-effects as it changes how the shell interprets text inside the quotes. – cas Nov 19 '21 at 01:51
  • 2
    Does this answer your question? How to escape quotes in shell? – cas Nov 19 '21 at 01:53
  • this is the error: sed: -e expression #1, char 42: unknown option to `s' – Sergio Baiao Nov 19 '21 at 01:58
  • @SergioBaiao answer below – AlexPixel Nov 19 '21 at 03:24
  • If the file is XHTML, it would be safer to do this with xmlstarlet. – Kusalananda Nov 19 '21 at 06:53
  • 3
    Don't use find and sed -i while you're still trying to get something to work. At best it probably won't break your source code too much, but until you know the edit expression does what you really want it to do there's a strong possibility it will warp your files beyond repair. Instead, use an instance of sed that operates on just a single file, and reivew its output carefully – Chris Davies Nov 19 '21 at 10:13