Trying to replace

window.location = '/loft-run'+ResourceManager.hotlegs + mainPage + ".html#" + newhash;


window.location = ResourceManager.hotlegs + mainPage + ".html#" + newhash;

in a file. what I have tried

sed -i 's~/loft-run'+ResourceManager.hotlegs + mainPage + ".html#" + newhash"~ResourceManager.hotlegs + mainPage + ".html#" + newhash"' warmblanket.js

Have tried some sed commands but not much of help. Your suggestions would be of great help.

  • 1
    The code you posted cannot be executed. Both the first and the second version. The reason is easy: There are three unmasked ' in the sed command line (most of which is not quoted at all so the whole line is really broken). Jul 16, 2020 at 23:51

2 Answers 2


Like this:

sed -i "s@'/loft-run'\+@@" warmblanket.js
  • the basic form for substitution is s/before/after/
  • use double quotes if you want to treat single quotes
  • chosen the @ delimiter here instead of basic /, you can pick up most of the ASCII table

sed doesn't know about literal strings, just regexps and backreference-enabled text. To do anything with literal strings just use a tool that understands them such as awk:

$ cat file
window.location = '/loft-run'+ResourceManager.hotlegs + mainPage + ".html#" + newhash;

$ awk \
    -v old="window.location = '/loft-run'+ResourceManager.hotlegs + mainPage + \".html#\" + newhash;" \
    -v new='window.location = ResourceManager.hotlegs + mainPage + ".html#" + newhash;' \
    's=index($0,old) { $0=substr($0,1,s-1) new substr($0,s+length(old)) } 1' file
window.location = ResourceManager.hotlegs + mainPage + ".html#" + newhash;

Note the use of " instead of ' around the old=... string because that string includes 's and shell doesn't allow 's inside a '-delimited string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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