Inserting text into a HTML file using Perl

S

Slain

I have a big list of HTML files, which need to be updated with a
common text.


<script language=JavaScript
src="./highlight.js"></script>

I need to add the above line in each of the html files, before the
text "<\head>". All the HTML files have this text and I think some
kind of search on this string and either replacing "</head>" with the
text above and appending "<\head> to it, might be one way to do it or
just find the <\head> part and insert the text before that.



On similar lines, the text below goes towards the end. The problem is
similar to the above one and I think all that needs to be done, is
read the text below from a file, do a similar search for some text
below which this insert part should go and put it there.


<p><script type="text/javascript">var mailSubject = 'Hardware
Overview';
var mailBody = 'Your Technical Support Team hopes this topic will
be helpful: ' + location.href;
var mailDisplay = 'Click here to email this topic.';
document.write(
'<a href="mailto:[email protected]'
+ '?subject=' + escape(mailSubject)
+ '&body=' + escape(mailBody)
+ '">' + mailDisplay + '</a>'
);</script></p>

<script type="text/javascript"
language=JavaScript1.2><!--
beginSearch ();
//--></script>


Any ideas would be helpful. I do not know much about perl and hence
the more the better.
Thanks a lot
 
J

J. Gleixner

Slain said:
I have a big list of HTML files, which need to be updated with a
common text.


<script language=JavaScript
src="./highlight.js"></script>

I need to add the above line in each of the html files, before the
text "<\head>". All the HTML files have this text and I think some
kind of search on this string and either replacing "</head>" with the
text above and appending "<\head> to it, might be one way to do it or
just find the <\head> part and insert the text before that.

perl -pi -e 's/search/replace/' *

If you have files in other directories, that './highlight.js' isn't
going to work, use a path relative to the document root.

And, if you have files in other directories...

perl -pi -e 's/search/replace/' `find ./ -name *.html`

Look up what that will/should do, before you run it.

On similar lines, the text below goes towards the end. The problem is
similar to the above one and I think all that needs to be done, is [...]
<p><script type="text/javascript">var mailSubject = 'Hardware [...]
//--></script>

Put that in a separate file, like you did with highlight.js, and use the
same technique as above.
Any ideas would be helpful. I do not know much about perl and hence
the more the better.

Then, write it in a language you do know.

BTW: Searching the Internet for 'search replace perl' would
have revealed a lot of helpful code and you'd probably be
finished with this by now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top