SED Search and Replace Question for Google Analytics

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 08-07-2007
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:

Code:
</body>

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

Code:
<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:

Code:
<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!
# 2  
Old 08-07-2007
The simplest way I can think of, given the embedded slashes is a substitution:
Code:
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

# 3  
Old 08-07-2007
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?
# 4  
Old 08-07-2007
Code:
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

# 5  
Old 08-07-2007
Thanks shell_life.

I also did it this way:

Code:
!/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!

Last edited by Neo; 08-07-2007 at 07:00 PM.. Reason: Changed typo "line liner" to "one liner"
# 6  
Old 08-07-2007
Quote:
Originally Posted by Neo
I was hoping to find a simple "line liner" vs. a shell script wrapper around sed. It seems there are no "one liners".
This seems to be the best solution for a "one liner".

Good luck.

Last edited by Shell_Life; 08-08-2007 at 03:37 PM.. Reason: "line liner" to "one liner".
# 7  
Old 08-07-2007
Unless using gsed...
in which case:

Code:
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 Smilie The alternative would be to replace the sed in the above expression with a 'perl -pi -e '.
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Another sed/awk search=>replace question f77coder Shell Programming and Scripting 21 08-31-2014 01:12 AM
Search and replace question reyes99 Shell Programming and Scripting 2 04-16-2013 10:09 AM
Need help with search and replace using SED wisam74us Shell Programming and Scripting 3 04-03-2012 01:51 AM
search replace regex question lawstudent Emergency UNIX and Linux Support 7 12-12-2011 03:23 AM
Sed - search and replace help. raz0r Shell Programming and Scripting 1 08-17-2011 06:07 PM
How to use 'sed' to search and replace? DallasT UNIX for Dummies Questions & Answers 4 02-10-2011 05:54 PM
Search & Replace question chatguy Shell Programming and Scripting 3 09-04-2010 05:41 PM
Bash sed search and replace question RickS Shell Programming and Scripting 3 08-09-2010 03:22 PM
SED Question: Search and Replace start of line to matching pattern DrivesMeCrazy Shell Programming and Scripting 3 03-18-2010 01:18 AM
sed search and replace fspalero Shell Programming and Scripting 2 09-01-2009 11:29 PM
search and replace one more question ghazi UNIX for Dummies Questions & Answers 2 06-20-2006 04:16 PM
Search/Replace with Sed mvalonso UNIX for Dummies Questions & Answers 5 05-08-2006 08:30 PM
sed search and replace d__browne UNIX for Dummies Questions & Answers 7 04-26-2006 10:46 AM
All times are GMT -4. The time now is 11:10 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.