SED Search and Replace Question for Google Analytics

Well, I'm losing my regex ability in sed! Please help.

I need to search for this text in multiple html files in a directory:

</body>

and add the following lines in front of the text above:

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>

<script type="text/javascript">
_uacct = "UA-1234567-00";
urchinTracker();
</script>

to finally read:

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>

<script type="text/javascript">
_uacct = "UA-1234567-00";
urchinTracker();
</script>
</body>

... for each file in the directory.

... and everything I try with sed keeps giving me errors.

Thanks for your help!

The simplest way I can think of, given the embedded slashes is a substitution:

sed 's*</body>*<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">\
</script>\
\
<script type="text/javascript">\
_uacct = "UA-1234567-00";\
urchinTracker();\
</script>\
</body>*' filename

Hi reborg!

Thanks. That works for a single file to standard out very well.

How about this operation for many files in a single directory? Do I need to wrap this in a shell script: read each file in the directory, write to a tmp file and then mv the tmp file to replace the original file?

Or can we do this with one line in sed?

mStr1='<script src="http://www.google-analytics.com/urchin.js"'
mStr2=' type="text/javascript"> </script> <script type="text/javascript"'
mStr3='> _uacct = "UA-1234567-00"; urchinTracker(); </script>'
for mFName in `find . -type f`
do
  while read mLine
  do
    if [ "${mLine}" = "</body>" ]; then
      echo ${Str1}
      echo ${Str2}
      echo ${Str3}
    fi
    echo ${mLine}
  done < ${mFName} > $$Temp
  mv $$Temp ${mFName}
done

Thanks shell_life.

I also did it this way:

!/bin/sh

for file in $(ls *.html)
do
sed 's*</body>*<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">\
</script>\
\
<script type="text/javascript">\
_uacct = "UA-1234567-00";\
urchinTracker();\
</script>\
</body>*' $file > /tmp/tmpfile.tmp
mv /tmp/tmpfile.tmp $file
done

I was hoping to find a simple "one liner" vs. a shell script wrapper around sed. It seems there are no "one liners".

Thanks for the suggestions!

This seems to be the best solution for a "one liner".

Good luck.

Unless using gsed...
in which case:

gsed -i 's*</body>*<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">\
</script>\
\
<script type="text/javascript">\
_uacct = "UA-1234567-00";\
urchinTracker();\
</script>\
</body>*' *.html

gets rid of the script :wink: The alternative would be to replace the sed in the above expression with a 'perl -pi -e '.

for i in *.txt
do
  ex - ${i} <<EOF
%s#foo#bar#g
wq!
EOF
# or with printf
# printf '%s#foo#bar#g\n.\nwq!\n' | ex "${i}"
done

Hi reborg! Thanks! I am on Mac OSX, which does not seem to have gsed. So, I'll stick with the wrapper for now, since it works fine.

Hi shell_life, I'll edit my post to fix my typo, LOL :slight_smile: Thanks again!

I edited while you were replying, maybe you have perl, I don't have a Mac handy (unless I'm in the office) to check.

Hi reborg!

Yes, that is the way I used to do years ago it before I forgot (with perl, not sed), thanks; and yes, OSX has perl out-of-the-box.

Thanks again! Mission accomplished!

find ./ -type f -exec sed 's*</body>*<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">\
</script>\
\
<script type="text/javascript">\
_uacct = "UA-1234567-00";\
urchinTracker();\
</script>\
</body>*' {} \;