0

Trying to replace

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

to

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
  • 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). Commented Jul 16, 2020 at 23:51

2 Answers 2

0

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
0
0

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.