1

i have a minified javascript file using r.js, i need to put the entire content of the file in another java file around the line 19, so i try this commands (Mac):

SRC_FILE=`cat ./javascript.minify.js`
sed "19s/.*/${SRC_FILE}/g" ./Original.java > ./Replace.java

the result of this command:

bad flag in substitute command: 'o'

so i try with perl:

SRC_FILE=`cat ./javascript.minify.js`
cat Original.java | perl -p -e "s/.*/'${SRC_FILE}'/g if $.==19" > ./Replace.java

the result of this command:

Bareword found where operator expected at -e line 1, near "bowels"
syntax error at -e line 1, near "bowels "
Can't find string terminator '"' anywhere before EOF at -e line 1.

how i can tell sed or perl that the replace is a string?

FIXED
the only way for this, is using base64:

SRC_FILE=`cat ./javascript.minify.js | base64`
sed "19s/.*/${SRC_FILE}/g" ./Original.java > ./Replace.java

thanks for your comments and everything

Alejo JM
  • 921
  • 7
  • 12
  • See [How to use shell variables in perl command call in a bash shell script?](http://stackoverflow.com/q/13093709/176646) – ThisSuitIsBlackNot Nov 12 '15 at 20:03
  • no the error for perl is: File name too long – Alejo JM Nov 12 '15 at 20:55
  • 1
    You said the error for Perl was `Bareword found where operator expected at -e line 1, near "bowels"`. Regardless, it would be better to read the JavaScript file using Perl instead of trying to store it in a Bash variable. – ThisSuitIsBlackNot Nov 12 '15 at 20:57
  • When you use `/.*/${SRC_FILE}/g` in either the sed or the perl case — does the string SRC_FILE (which is the contents of javascript.minify.js) contain *any* slashes `/`? I expect it does, and when `${SRC_FILE}` gets replaced with its content, now your regex contains too many slashes, and becomes invalid. – Stephen P Nov 12 '15 at 23:25
  • _"the only way for this, is using base64"_ — that's certainly not the _only_ way. You want to insert the contents of a file at a certain location in another file (?); regex seems a poor choice to do that - you're not matching and replacing a string, this is not a pattern problem. `sed` can do much more than `s/this/that/`— it could `sed '19a Some New Text'` to append text at line 19. `sed` is not limited to commands on the command line; it can have a whole script file, including branching. `awk` is another good possibility, and `perl` just doing file processing _without_ using regex. `man sed` – Stephen P Nov 13 '15 at 18:56
  • See [this answer](http://stackoverflow.com/a/16715488/17300) on http://stackoverflow.com/questions/16715373/sed-insert-file-content-after-specific-pattern-match – Stephen P Nov 13 '15 at 18:59

1 Answers1

0

Sed can directly read the contents of a file into another file using the r command, without requiring an intermediate shell variable which introduces a nightmare of quoting and escaping problems.

This goes to line 19 and reads file javascript.minify.js at that point:

sed '19 r javascript.minify.js' ./Original.java > ./Replace.java

This adds the contents of the .js file after line 19 in Original.java while your original replaces the content of line 19. To achieve the same effect you can delete line 19 after you've added the file contents:

sed -e'19 r javascript.minify.js' -e'19 d'  ./Original.java > ./Replace.java
Stephen P
  • 14,422
  • 2
  • 43
  • 67