Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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)?
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
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
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
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 ... :}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.