Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 08-07-2007
Neo's Avatar
Neo Neo is offline Forum Staff  
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 7,778
Thanks: 16
Thanked 31 Times in 17 Posts
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!
Sponsored Links
  #2  
Old 08-07-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,466
Thanks: 0
Thanked 1 Time in 1 Post
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
Neo's Avatar
Neo Neo is offline Forum Staff  
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 7,778
Thanks: 16
Thanked 31 Times in 17 Posts
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
Shell_Life's Avatar
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Thanks: 0
Thanked 0 Times in 0 Posts

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
Neo's Avatar
Neo Neo is offline Forum Staff  
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 7,778
Thanks: 16
Thanked 31 Times in 17 Posts
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 06:00 PM.. Reason: Changed typo "line liner" to "one liner"
  #6  
Old 08-07-2007
Shell_Life's Avatar
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Neo View Post
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 02:37 PM.. Reason: "line liner" to "one liner".
  #7  
Old 08-07-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,466
Thanks: 0
Thanked 1 Time in 1 Post
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 The alternative would be to replace the sed in the above expression with a 'perl -pi -e '.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search and replace one more question ghazi UNIX for Dummies Questions & Answers 2 06-20-2006 03:16 PM
A google search shellscript JoeTheGuy Shell Programming and Scripting 3 10-24-2002 04:57 PM



All times are GMT -4. The time now is 07:38 AM.