Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
I am not sure who wrote it but I had to use parts of it so that I could replace information in about 140 or so files. Just wanted to share it with LQ in case anyone was ever in need.
Code:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
my $startdir = '/starting dir/';
my $find = 'find';
my $replace = 'replaces';
my $doctype = 'txt or php or pl or log';
print qq~Finding "$find" and replacing it with "$replace"\n~;
find(
sub{
return unless (/\.$doctype$/i);
local @ARGV = $_;
local $^I = '.bac';
while( <> ){
if( s/$find/$replace/ig ) {
print;
}
else {
print;
}
}
}, $startdir);
print "Finished";
Also it could be rewritten if needed to be for say a global file replacement like changing .jpeg to jpg.
sorry about the path line I actually used it in a production windows environment because I needed to rename a lot of things in windows. So I used activestate perl which adds the perl binary to your path.
Last edited by jstephens84; 02-04-2009 at 08:03 AM.
First, let me thank you for taking time to (1) create/modify something and then (2) share with the community. That is what makes the OSS movement so powerful.
Next, I'd like to COMMENT about your script.
1. The "she-bang statement" on line #1, typically uses a fully qualified path to the executable involved. In your case, "/usr/bin/perl". Without the path, you rely on the assumption that whoever uses your script will have perl on their $PATH exactly as you do. This might not be the case and the script will not launch.
2. My other COMMENT has to do with your use of comment statements in the script code. As a veteran of over 30 years slinging code, I have never regretted comment statement that I've added to my code. On the other hand, I have routinely cursed the comment statement that were missing or were misleading. If you didn't use them because of typing troubles, typing practice will address that concern. Don't worry about the extra storage space for those bytes of baggage characters. Even a terabyte drive is little over $100 USD these days. Lastly, scripting languages like perl are interpreted and so there are wasted cycles to skip past comments. Yes, but cycles are near zero cost compared to your time -- code comes back to haunt its author -- to rediscover what you meant by some cryptic statement or variable.
Thanks, again, and keep up the good work.
~~~ 0;-D
PS/ the script works for me after fixing she-bang.
BTW does any body know how to get line numbers?
I mean when it says like there is a problem in line 25, do I count every line?
Do I include spaces?
do I include # rems?
or ??
BTW does any body know how to get line numbers?
I mean when it says like there is a problem in line 25, do I count every line?
Do I include spaces?
do I include # rems?
or ??
thanks!
It goes through all the files in a particular directory and its sub directories and will look for what is in the find and replace it with what you have in the replace variable. The doctype is used to specify what file extension it should look for. So if you want to change files with a .txt then use that.
This application would be great for developers that have mass amounts of config files that needed say a server name changed to another server name or for an admin who is moving an application from one server to another (which is what I used it for.)
Yeah I am very well aware of that. Only problem is that I know a little perl and less sed and awk. But that is about to change. I am probably going to get a book on sed and awk but I am holding off because I have been reading and so far pretty much anything that can be done with awk and sed and can be done with perl.
AFAIK perl is much more flexible and powerful, so you could probably stick with it quite happily. But for these specialised tasks, it's nice to be able to write a quick one-liner : )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.