LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find and Replace (https://www.linuxquestions.org/questions/linux-newbie-8/find-and-replace-552667/)

upwordz 05-09-2007 01:31 PM

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' {} \;

XavierP 05-09-2007 02:09 PM

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)?

upwordz 05-09-2007 02:21 PM

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

Tinkster 05-09-2007 07:12 PM

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

cheeseandpenguins 05-10-2007 07:43 AM

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!

Tinkster 05-10-2007 01:18 PM

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

upwordz 05-10-2007 04:09 PM

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

Tinkster 05-10-2007 09:03 PM

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


All times are GMT -5. The time now is 06:19 PM.