2

Is there any bash solution to do a simple "search and replace" without escaping hassle? I try to replace <!-- JavaScript --> in an html file with the content of a complex javascript file.

I have tried

JS=$(<"path to javascript file")
sed "s|<!-- JavaScript -->|${JS}|g" "path to html file" > "path to html file"

but just get

sed: -e Ausdruck #1, Zeichen 16: Nicht beendeter `s'-Befehl

In Powershell I do

$CSS = Get-Content "path to javascript file"
(Get-Content "path to html file").replace('<!-- JavaScript -->', $JS) | Set-Content "path to html file" -Force

and it just works like a charm, without escaping hassle.

Update (but doesn't work too):

JS=$(<"${TemporaryPath}/${Project}/${Project}.js")
E='!'
sed "s|<${E}-- JavaScript -->|${JS}|g" "path to html file" > "path to html file"

I get sed: -e Ausdruck #1, Zeichen 68: Nicht beendeter s'-Befehl. If I change the content of $JS to something basic like "foo" it works. Probably a problem with the javascript content of $JS? What can I do that the content of $JS doesn't matter?

migrated from serverfault.com Jan 14 '16 at 15:03

This question came from our site for system and network administrators.

  • I believe, if you use single quotes instead of double with sed, that would do the trick) another way would be to use variables, where you defined before running sed. – alexus Jan 12 '16 at 17:23
  • But with single quotes the ${JS} variable will not be replaced? – Kiltarc Jan 12 '16 at 17:27
  • no it will not, but regardless I'd suggest run some tests and make sure you make a backup as well! – alexus Jan 12 '16 at 17:28
  • sed 's|'"<${E}"'-- JavaScript -->|'"${JS}|g"'' might do the trick (instead of double qoutes use single qoutes for the whole sed string, only "excluding" the variables, though this is from memory only and might need more. – Dennis Nolte Jan 13 '16 at 8:37
3

Your problem is that bash is interpreting the exclamation mark. Escaping that with a backslash unfortunately does not work. Put it in another variable, or put it in single quotes while keeping your variable in double quotes, and all will be well . . .

$ ARG="mytest"
$ echo "hello $ARG!"
bash: !": event not found
$ # didn't work
$ echo "hello $ARG\!"
hello mytest\!
$ # didn't work either!
$ echo "hello $ARG"'!'
hello mytest!
$ # that's better
$ E='!'
$ echo "hello $ARG$E"
hello mytest!
$ I like this one best.
  • I have updated my question to your suggestions, but it doesn't work. I get sed: -e Ausdruck #1, Zeichen 68: Nicht beendeter s'-Befehl. If I change the content of $JS to something basic like "foo" it works. Probably a problem with the javascript content of $JS? What can I do that the content of $JS doesn't matter? – Kiltarc Jan 13 '16 at 8:29
  • Missed two things: if you are trying to replace in a file, use sed -i -e 's|x|y|' $filename. The other thing is worse: you are actually replacing with a very long string that certainly contains newlines and probably also | characters and possibly whatever other characters you may choose as sed separators. sed is not a good tool for your problem. There are others, but before trying something, doesn't (can't) your webserver do this kind of replacement automatically? – Law29 Jan 13 '16 at 10:03
  • That does the trick! The file contains some | characters. Now I use # as seperator. – Kiltarc Jan 13 '16 at 11:21
  • If it works, then that's good! If you need something more complicated, you may wish to look at m4 macros, or at a shell script to output the beginning of the file, the replacement file, and then the rest of the file, or (probably the best option) at the possibility that your webserver might be able to do the insertion as the page is served. – Law29 Jan 13 '16 at 13:09
0

use single quotes instead of double:

[alexus@wcmisdlin02 Downloads]$ test="testing"
[alexus@wcmisdlin02 Downloads]$ echo "$test"
testing
[alexus@wcmisdlin02 Downloads]$ echo '$test'
$test
[alexus@wcmisdlin02 Downloads]$ 
  • As you see echo '$test' doesn't replace the variable.. – Kiltarc Jan 12 '16 at 17:44

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.