LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-09-2007, 01:31 PM   #1
upwordz
LQ Newbie
 
Registered: May 2007
Location: Portland Maine
Distribution: Red Hat 2.6.9-34.EL, FC3, Cent5
Posts: 8

Rep: Reputation: 0
Find and Replace


So here is what I want to accomplish. I want to add the Google Analytics tracking code to all my pages on the site. They tell you to add it just before the ending body tag. So my thought was, ok, I want to do a find and replace on </body> with the code and the ending tag. Question is how do I do that from the command line. I only have ssh access to my server. I do have root access and I know where all the website files are located, so I was thinking of starting at the top of the parent directory for those files and firing off some recursive command to look for php and htm and html files with the ending body tag and replacing it with my tracking code and ending tag. I went hunting through the forums here and found a good starting command in this thread: http://www.linuxquestions.org/questi...d.php?t=346190. Below is my attempt to customize the command but I recognize a couple of problems. One, I probably need to escape forward slashes and other characters in the script. I also would like to include htm and html extensions and didn't know how to group them. Thanks for your time any any help! Darren

find /PATH -name "*php" -exec sed -i 's/</body>/<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript">
_uacct = "########"; urchinTracker(); </script></body>/g' {} \;

Last edited by upwordz; 05-09-2007 at 02:16 PM.
 
Old 05-09-2007, 02:09 PM   #2
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Surely, by doing that you will be putting the code into every page in your site? So, index.php, single.php, comment.php, sidebar.php (assuming that this is a blog, of course)?
 
Old 05-09-2007, 02:21 PM   #3
upwordz
LQ Newbie
 
Registered: May 2007
Location: Portland Maine
Distribution: Red Hat 2.6.9-34.EL, FC3, Cent5
Posts: 8

Original Poster
Rep: Reputation: 0
True. I hadn't spent too much time considering the implications. It's not a blog site, it's an ecommerce website and while there are some include pages, like a header script and a footer script, the tracking numbers for those scripts will be abnormally high but it would be easier to add the script to all our pages and then remove the ones we dont want, then to write a command to capture only the ones we do. Right? I don't have my hands around all the files that would be affected by this edit but where our product pages and category result pages are all dynamic, I think the number would be reasonable, less than 500. Is there a way to modify the command to report the files affected? That would be helpful. Darren
 
Old 05-09-2007, 07:12 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Just remove the exec-statement ...
Code:
find /PATH -name "*php"
You could conceivably take the output of that, put it in a
file, sift through that and then let sed loose on the list
of files you actually want to modify.
Code:
for i in `cat list`
do
  sed -i 's@</body>@<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript"> _uacct = "########"; urchinTracker(); </script></body>@g'
done
Cheers,
Tink
 
Old 05-10-2007, 07:43 AM   #5
cheeseandpenguins
Member
 
Registered: May 2007
Distribution: suse 10.1, fedora 6
Posts: 39

Rep: Reputation: 15
can't u press enter while u r writing something...
it's highly annoying to have to look accross as well as up and down!
 
Old 05-10-2007, 01:18 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by cheeseandpenguins
can't u press enter while u r writing something...
it's highly annoying to have to look accross as well as up and down!
I assume that you're talking to me?

Firstly, this method makes it possible for
others to copy & paste the code w/o having
to remove the line-breaks.

Secondly, you're very rude, and your post has
no relation to the thread; if you have a problem
with my style send me an e-Mail.

Thirdly, can't you use proper English and the
spell-checker?

Fourthly, how about a sensible ratio between
payload and signature? 120 characters vs 381?



Cheers,
Tink
 
Old 05-10-2007, 04:09 PM   #7
upwordz
LQ Newbie
 
Registered: May 2007
Location: Portland Maine
Distribution: Red Hat 2.6.9-34.EL, FC3, Cent5
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks Tinkster, let me ask you this. I put the results of the find files into a file with: find /PATH -name "*php" > /PATH/findfiles.txt

Then I went through the file and trimmed it down. Now you said to
Quote:
let sed loose on the list of files you actually want to modify.
with
Code:
for i in `cat list`
do
  sed -i 's@</body>@<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript"> _uacct = "########"; urchinTracker(); </script></body>@g'
done
Stupid question, would this be a bash script I would be writing to run the code you provided or something else? Thanks again! Darren
 
Old 05-10-2007, 09:03 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
It is indeed ... you can either save it with the bash-shebang line,
or just use it as a one-liner if you append a \ to each of the lines.

I split it up like this for use in a script, though.

Code:
#!/bin/bash
for i in `cat list`
do
  sed -i 's@</body>@<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript"> _uacct = "########"; urchinTracker(); </script></body>@g'
done
is the "full version" of it. Sorry about the misunderstanding.
It is, of course, untested, and I highly recommend letting it
loose on a copy of the directory structure, or some test-files
first ... :}


Cheers,
Tink
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace string Johng Programming 9 01-13-2010 04:50 AM
find and replace script UnixKiwi Programming 12 04-16-2007 11:08 PM
find and replace happy78 Programming 11 09-10-2005 10:21 AM
Help - how to find and replace in Vim stardotstar Linux - Software 7 10-14-2004 11:31 PM
Find and Replace? duerra Linux - General 9 01-28-2004 04:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration