I have a javascript file with a jquery function call:
$.getScript('/scripts/files/file.js');
I want to replace that line with the contents of the file at that path. This is the bash script I have so far:
cat public/scripts/old.js | sed -e "s/$\.getScript\('(.)+'\);/$(cat \1)/g" > public/scripts/new.js
However, my regular expression and remembering the path does not seem to be working correctly. I get cat: 1: No such file or directory as it seems as if
cat is being called on the number 1 (which should be the remembered portion of the regexp). How can I fix this?
Because you are using
$()inside double quotes, the shell is parsing thecat \1, stripping the backslash and trying to runcat 1to pass its output as part of the argument to sed. Sed has a command (r) for reading a file, but the filename must be literal, and cannot be the result of previous sed commands (at least in standard sed, perhaps some implementations provide that ability).sedis really the wrong tool for this. You could do an awk solution, but it will be fragile.Here’s a possible perl solution (warning: fragile):