-   Linux - General (
-   -   Great Perl script that I had to share (

jstephens84 02-03-2009 10:33 AM

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.

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~;
                return unless (/\.$doctype$/i);
                local @ARGV = $_;
                local $^I = '.bac';
                while( <> ){
                        if( s/$find/$replace/ig ) {
                        else {
        }, $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.

SaintDanBert 02-03-2009 04:54 PM

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.

chrism01 02-03-2009 08:26 PM


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
See also the runtime flags

Matey 02-04-2009 07:12 AM

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


chrism01 02-04-2009 07:26 AM

If you use vi/vim, you can use

to get to line 25. You can also add

set ruler
to your .vimrc which will always show you what line/char you are in/on.

cat -n

jstephens84 02-04-2009 08:05 AM


Originally Posted by Matey (Post 3431789)
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 ??


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

openSauce 02-04-2009 01:18 PM

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

find $startdir \( -name "*.txt" -o -name "*.php" -o -name "*.pl" -o -name "*.log" \) -exec sed -i "s/$find/$replace/g" \{\} \;

jstephens84 02-04-2009 02:12 PM

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.

openSauce 02-04-2009 02:19 PM

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

chrism01 02-04-2009 07:28 PM

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

All times are GMT -5. The time now is 01:34 PM.