My input:

sed -i 's/if ($.browser.msie)/if ($.browser={ msie: ( navigator.appName == 'Microsoft Internet Explorer') ? true : false })/g' file.js

I have tried putting the search and the replace text in both single and double quotes, but it gives the error:

sed: -e expression #1, char 79: unterminated `s' command
  • Insert whitespace before filename file.js. – Cyrus Jan 11 '15 at 11:08
  • There is a space before the filename, but inputing it here removed it :( – Johndoe Jan 11 '15 at 11:12

Replace the single quotes around Microsoft Internet Explorer with double quotes and insert a space character before file.js as you mentioned in your comment that you already did. Also use a backslash character to escape the $ and . characters by putting a backslash in front of them where they occur in the regex.

sed -i 's/if (\$\.browser\.msie)/if (\$\.browser={ msie: ( navigator\.appName == "Microsoft Internet Explorer") ? true : false })/g' file.js
  • That works, and fortunately the code works with double rather than single quotes. How to get around it if the single quotes are required, example: sed -i 's/$pathTo = '';/$pathTo ='/usr/local/bin/somewhere';/g' file.php which gives the error: sed: -e expression #1, char 25: unknown option to `s' – Johndoe Jan 11 '15 at 13:08
  • Use this command: sed -i 's/\$pathTo = '\'\'';/\$pathTo ='\''\/usr\/local\/bin\/somewhere'\'';/g' file.php Here's how it works: 1. Write the regex between single quotes. 2. Inside the regex use '\'' instead of a single quote. 3. Put a backslash before $.*/\^ (and also the 2 square bracket characters) and only those characters. For more information see What characters do I need to escape when using sed in a sh script?. – karel Jan 11 '15 at 14:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.