|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
Quote:
Good luck. Last edited by Shell_Life; 08-08-2007 at 02:37 PM.. Reason: "line liner" to "one liner". |
|
#7
|
||||
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |