LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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

Reply
 
Search this Thread
Old 02-03-2009, 10:33 AM   #1
jstephens84
Senior Member
 
Registered: Sep 2004
Distribution: (Home)Opensolaris, Ubuntu, CentOS, (Work - AIX, HP-UX, Red Hat)
Posts: 2,043

Rep: Reputation: 83
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.
 
Old 02-03-2009, 04:54 PM   #2
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,347
Blog Entries: 3

Rep: Reputation: 86
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.
 
Old 02-03-2009, 08:26 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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
 
Old 02-04-2009, 07:12 AM   #4
Matey
Member
 
Registered: Jan 2009
Posts: 108

Rep: Reputation: 17
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.
 
Old 02-04-2009, 07:26 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
If you use vi/vim, you can use
Code:
:25
to get to line 25. You can also add
Code:
set ruler
to your .vimrc which will always show you what line/char you are in/on.
Alternately,
Code:
cat -n file.pl
 
Old 02-04-2009, 08:05 AM   #6
jstephens84
Senior Member
 
Registered: Sep 2004
Distribution: (Home)Opensolaris, Ubuntu, CentOS, (Work - AIX, HP-UX, Red Hat)
Posts: 2,043

Original Poster
Rep: Reputation: 83
Quote:
Originally Posted by Matey View Post
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.)
 
Old 02-04-2009, 01:18 PM   #7
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
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" \{\} \;
 
Old 02-04-2009, 02:12 PM   #8
jstephens84
Senior Member
 
Registered: Sep 2004
Distribution: (Home)Opensolaris, Ubuntu, CentOS, (Work - AIX, HP-UX, Red Hat)
Posts: 2,043

Original Poster
Rep: Reputation: 83
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.
 
Old 02-04-2009, 02:19 PM   #9
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
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 : )
 
Old 02-04-2009, 07:28 PM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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
 
  


Reply

Tags
example, perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
it'd be great if all experts are willing to share their experience. htetnaing Linux - Newbie 15 08-20-2008 11:11 AM
NEED HELP IN comment lines PERL Perl script adam_blackice Programming 17 11-07-2007 08:01 AM
connect to windows share through smb://works great in gnome explorer,lousy in KDE? zeltak Linux - Software 0 01-09-2007 01:46 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM


All times are GMT -5. The time now is 05:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration