0

I want to perform a find and replace in my 8,000 (plus) records (html files) but the problem is the string that i will replace contains spaces.

I want to find all files that has an instance of </head> so I could insert my google analytics before the <head> tag closes.

my google Analytics looks like this:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'XX-XXXXXXXX-X']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

after finding </head> the result should look like this:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'XX-XXXXXXXX-X']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</head>

On my server, I have FTP & SSH access. What approach should I use to replace them fast and lightweight?

Thank You.

Pennf0lio
  • 3,888
  • 8
  • 46
  • 70

2 Answers2

2

use sed over all html files.

sed 's/<\/head>/before end head/' input.html

just replace 'before end head' as your need. don't forget to use \ properly.

Sarowar
  • 452
  • 4
  • 14
1

You want the sed append command.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • Thanks for the suggestion... would you mind providing some example how to do it? I'm not familiar with the command. – Pennf0lio Jun 01 '12 at 21:17