dr jerry dr jerry - 1 year ago 59
Perl Question

Looking for a Perl or sed one-liner to replace a string with contents from another file

In an HTML file, let’s call it

, 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 one-liner 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 development environment).

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download