Take the 2-minute tour ×

In a html file, lets call it index.html I want to replace a comment string say //gac goes here with the contents (multi-line) from a separate file which is called: gac.js. Is there a nice oneliner for that?

I found something as: sed -e "/$str/r FileB" -e "/$str/d" FileA but it is not working as promised

I do like it as short as possible, As it will be called after an svn revert (I don't want any of that google.script polluting my dev environment).

share|improve this question
is the contents of the file gac.js multi-lined or sinle line? –  Andrey Adamovich Jun 21 '11 at 18:36
gac.js is multi-line (edited) –  dr jerry Jun 21 '11 at 18:42
That sed command works for me. What goes wrong? –  Beta Jun 21 '11 at 22:41
Not very experienced with sed but i think i did something wrong with the slash forwards. –  dr jerry Jun 22 '11 at 8:01

4 Answers 4

up vote 4 down vote accepted

This should work, even though it is nasty:

perl -pe 'BEGIN{open F,"gac.js";@f=<F>}s#//gac goes here#@f#' index.html

In the case that gac.js is supposed to be dynamic:

perl -pe 's#//(\S+) goes here#open+F,"$1.js";join"",<F>#e' index.html
share|improve this answer
Thanks for this nasty one liner! –  Raj Mar 28 '13 at 20:00

After going through man sed, this tutorial and some experimenting I came up with:

sed -i '\_//gac goes here_ {
    r gac.js
}' index.html

Which does exactly what I want. It's not exactly a oneliner (if i make it one line i get: sed: -e expression #1, char 0: unmatched '{') which I don't understand. However expression above fits nicely in my update script.

Lessons learned: sed is very powerfull, -i behaves different on mac os x / linux, /string/ can easily be replaced with \[other delimiter]string[other delimiter].

share|improve this answer
The reason the '{' is unmatched is that sed takes everything from the r to the newline as the filename, so the closing brace is interpreted as part of the filename. –  William Pursell Jun 22 '11 at 12:32
@William Pursell Thank you! –  dr jerry Jun 23 '11 at 6:15
perl -mFile::Slurp -pe 's/\/\/(\w+) goes here/@{[File::Slurp::read_file("$1.js")]}/;'

Obviously requires File::Slurp

share|improve this answer

Not very nice, but seems to work:

cat index.html | perl -pe 'open(GAC, "gac.js");@gac=<GAC>;$data=join("", @gac); s/gac goes here/$data/g'
share|improve this answer
I think gac is supposed to be dynamic. –  Seth Robertson Jun 21 '11 at 18:54
Useless use of cat (partmaps.org/era/unix/award.html#cat) and you are reading in gac.js every time you read a line from index.html –  Chas. Owens Jun 21 '11 at 18:54
@Chas Owens, your version is better :). +1 for that –  Andrey Adamovich Jun 21 '11 at 18:59

Your Answer


