Linux - General This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-03-2009, 10:33 AM
|
#1
|
|
Senior Member
Registered: Sep 2004
Distribution: (Home)Opensolaris, Ubuntu, CentOS, (Work - AIX, HP-UX, Red Hat)
Posts: 2,043
Rep:
|
Great Perl script that I had to share
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.
|
|
|
|
02-03-2009, 04:54 PM
|
#2
|
|
Senior Member
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-12 & Cinnamon, Mint-11, v11.04 Ubuntu & Kubuntu
Posts: 1,116
Rep:
|
thanks for the effort ... and some COMMENTS
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.
|
|
|
|
02-03-2009, 08:26 PM
|
#3
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,026
|
Quote:
|
Lastly, scripting languages like perl are interpreted
|
Actually, Perl isn't interpreted, so that doesn't apply (wasting time skipping comments).
The perl program itself 'compiles' your script on the fly and runs that.
For more details http://www.perl.com/doc/FMTEYEWTK/comp-vs-interp.html
See also the runtime flags http://perldoc.perl.org/perlrun.html
|
|
|
|
02-04-2009, 07:12 AM
|
#4
|
|
Member
Registered: Jan 2009
Posts: 107
Rep:
|
what is this supposed to do any way?
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!
Last edited by Matey; 02-04-2009 at 07:18 AM.
|
|
|
|
02-04-2009, 07:26 AM
|
#5
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,026
|
If you use vi/vim, you can use
to get to line 25. You can also add
to your .vimrc which will always show you what line/char you are in/on.
Alternately,
|
|
|
|
02-04-2009, 08:05 AM
|
#6
|
|
Senior Member
Registered: Sep 2004
Distribution: (Home)Opensolaris, Ubuntu, CentOS, (Work - AIX, HP-UX, Red Hat)
Posts: 2,043
Original Poster
Rep:
|
Quote:
Originally Posted by Matey
what is this supposed to do any way?
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.)
|
|
|
|
02-04-2009, 01:18 PM
|
#7
|
|
Member
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252
Rep:
|
I'll second DanBert's thanks. However, did you know you can do the same thing with sed, which will also allow you to use regexes?
To get exactly the functionality of that script (as I understand it - I don't read perl), you could enter
Code:
find $startdir \( -name "*.txt" -o -name "*.php" -o -name "*.pl" -o -name "*.log" \) -exec sed -i "s/$find/$replace/g" \{\} \;
|
|
|
|
02-04-2009, 02:12 PM
|
#8
|
|
Senior Member
Registered: Sep 2004
Distribution: (Home)Opensolaris, Ubuntu, CentOS, (Work - AIX, HP-UX, Red Hat)
Posts: 2,043
Original Poster
Rep:
|
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.
|
|
|
|
02-04-2009, 02:19 PM
|
#9
|
|
Member
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252
Rep:
|
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 : )
|
|
|
|
02-04-2009, 07:28 PM
|
#10
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,026
|
Yeah, Perl is based on C, shell, awk and sed. There are even tools to cvt awk to perl (a2p) and sed to perl (s2p).
http://perldoc.perl.org/perl.html
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:29 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|