SED Search and Replace Question for Google Analytics


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED Search and Replace Question for Google Analytics
# 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 or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Web Development

Discrepancies Between Our Mobile Detection Code and Google Analytics Regarding Chinese User Agents

Update: Last two days the number of users on the site has peaked (normally around 10AM US Eastern Time) between 4,300 and 4,500. This is the highest number of consistent concurrent users in at least 3 years. ... and the traffic continues to rise week-over-week. (5 Replies)
Discussion started by: Neo
5 Replies

2. Shell Programming and Scripting

Another sed/awk search=>replace question

Hello, Need a little bit of help. Basically I need to replace lines in a file which were calculated wrong as it would 12 hours to regenerate the data. I need to calculate values based on other files which I've managed to figure out with grep/cut but now am stuck on how to shove these new... (21 Replies)
Discussion started by: f77coder
21 Replies

3. Shell Programming and Scripting

Search and replace question

Hi all, I am trying to modify an xml file and I wanted to search and replace using the sed command but here is my issue. I want to search and replace maximumHeapSize="512" and replace it with maximumHeapSize="768" but I have multiple files with different values so I can't search for... (2 Replies)
Discussion started by: reyes99
2 Replies

4. Emergency UNIX and Linux Support

search replace regex question

Hi, I need to run a search and replace on a large database, what I need to change is all instances of #### (eg. 1764 or 1964) to (####) (eg. (1764) or (1964)) But there might be other numbers in there such as (1764) and I do not need those changed to ((1764)) How can I... (7 Replies)
Discussion started by: lawstudent
7 Replies

5. Shell Programming and Scripting

Search & Replace question

Hi all, I have one question that hopefully isn't too complicated for the more advanced users here. In one of the Solaris KSH scripts I'm working on, is it possible to script the following: - If there "is" an empty blank line "at the end" of /tmp/text.txt, then remove only that one empty... (3 Replies)
Discussion started by: chatguy
3 Replies

6. Shell Programming and Scripting

Bash sed search and replace question

I have several files that I need to modify using sed. I know how to do that, but now a new requirement has come up. Now, I need to make changes to all lines that don't start with certain strings. For example, I need to change all lines except for lines that start with "a", "hello there",... (3 Replies)
Discussion started by: RickS
3 Replies

7. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

8. UNIX for Dummies Questions & Answers

search and replace one more question

Hi, I have a file that contains the following contents: 14:05 apple orange123 456mango 16:45 banana I wanted to replace ONLY the "14:05 " and "16:45" with nothing and trying to use the following syntax sed -e 's/*//g' -e 's/^: //g' my_file > new_temp cat new_temp apple orange... (2 Replies)
Discussion started by: ghazi
2 Replies
Login or Register to Ask a Question