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


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    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-07-2007
Neo's Avatar
Neo Neo is online now Forum Staff  
Administrator
 
Join Date: Sep 2000
Location: Asia pacific region
Posts: 11,509
Thanks: 308
Thanked 617 Times in 256 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 Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 8 Times in 8 Posts
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

Sponsored Links
    #3  
Old 08-07-2007
Neo's Avatar
Neo Neo is online now Forum Staff  
Administrator
 
Join Date: Sep 2000
Location: Asia pacific region
Posts: 11,509
Thanks: 308
Thanked 617 Times in 256 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: 1,203
Thanks: 1
Thanked 101 Times in 98 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

Sponsored Links
    #5  
Old 08-07-2007
Neo's Avatar
Neo Neo is online now Forum Staff  
Administrator
 
Join Date: Sep 2000
Location: Asia pacific region
Posts: 11,509
Thanks: 308
Thanked 617 Times in 256 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"
Sponsored Links
    #6  
Old 08-07-2007
Shell_Life's Avatar
Registered User
 
Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 101 Times in 98 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".
Sponsored Links
    #7  
Old 08-07-2007
reborg's Avatar
reborg reborg is offline Forum Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 8 Times in 8 Posts
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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search & Replace question chatguy Shell Programming and Scripting 3 09-04-2010 04:41 PM
Bash sed search and replace question RickS Shell Programming and Scripting 3 08-09-2010 02:22 PM
search and replace one more question ghazi UNIX for Dummies Questions & Answers 2 06-20-2006 03:16 PM



All times are GMT -4. The time now is 09:31 AM.