Ask a Question related to PERL Beginners, Design and Development.
-
Chetak Sasalu #1
Search Replace in multiple files
Hi,
I have to search and replace 'foo' to 'bar' in all the files in a
directory(has subdirectories).
The files are about 40k in size.
On the command line I would do it as,
find ./mydir/ -type f -print | xargs perl -pi -e 's/foo/bar/'
No backup of the original files required.I am brave.
What is the most efficient way to implement this inside a perl program ?
There are about 30 files to be processed.
I went through perldoc perlrun and saw the code.
I thought it as a criminal waste of time to try and modify that code for
my purpose, when I can ask you folks :-)
TIA,
Chetak
PS: I see this term 'foo' 'bar' in many programming books, what is the
etymology of this?
Confidentiality Notice
The information contained in this electronic message and any attachments tothis message are intended
for the exclusive use of the addressee(s) and may contain confidential orprivileged information. If
you are not the intended recipient, please notify the sender at Wipro [email]orMailadmin@wipro.com[/email] immediately
and destroy all copies of this message and any attachments.
Chetak Sasalu Guest
-
Search and replace
I'm certain this has been asked 1000 times.. however ... I recently imported some data - all INTs ... from an excel sheet, but the damn thing... -
Search and Replace - Best Approach
Hundreds of HTML file are generated by a program that includes in every file big chunks of comments, unused DHTML and Javascript functions. Always... -
Search and replace (super global replace)
I am using the 30 day trail of acrobate professional....before I buy it I have a few questions.... 1) is there a "search and replace" function... -
search an replace
Hi This scripts sucks in a 109mb file and i'm trying to do a search and replace on unxtime to the format from strftime. Which is working... ... -
search replace
Hi , How do I search replace text in a file from a perl script. i.e. by opening the file in write mode and then do a search replace. I don't... -
Jan Eden #2
Re: Search Replace in multiple files
[email]chetak.sasalu@wipro.com[/email] wrote:
I don't know if this is the most efficient way, but it works:>Hi,
>
>I have to search and replace 'foo' to 'bar' in all the files in a
>directory(has subdirectories). The files are about 40k in size.
>
>What is the most efficient way to implement this inside a perl
>program ? There are about 30 files to be processed. I went through
>perldoc perlrun and saw the code.
>
#!/usr/bin/perl -w
use strict;
use File::Find;
find \&process, $ARGV[0];
sub process {
$filename = $File::Find::name;
open IN, "$filename" or die "Couldn't open $filename for reading: $!";
$^I = "";
while (<IN>) {
s/foo/bar/g;
}
close IN;
print "Updated file $filename\n";
}
- Jan
--
If all else fails read the instructions. - Donald Knuth
Jan Eden Guest
-
Jan Eden #3
Re: Search Replace in multiple files
Please disregard my post, I mixed up the magic <> and the less magic <FILEHANDLE>.
--
These are my principles and if you don't like them... well, I have others. - Groucho Marx
Jan Eden Guest
-
Rob Dixon #4
Re: Search Replace in multiple files
Chetak Sasalu wrote:
use strict;>
> I have to search and replace 'foo' to 'bar' in all the files in a
> directory(has subdirectories).
> The files are about 40k in size.
>
> On the command line I would do it as,
> find ./mydir/ -type f -print | xargs perl -pi -e 's/foo/bar/'
>
> No backup of the original files required.I am brave.
>
> What is the most efficient way to implement this inside a perl program ?
> There are about 30 files to be processed.
> I went through perldoc perlrun and saw the code.
use warnings;
local @ARGV = grep -f, <./mydir/*>;
$^I = '';
while (<>) {
s/foo/bar/g;
print;
}
Rob
Rob Dixon Guest
-
Randy W. Sims #5
Re: Search Replace in multiple files
On 02/06/04 07:45, [email]chetak.sasalu@wipro.com[/email] wrote:
It is derived from a technical acronym FUBAR - F@#!ed Up Beyond All> PS: I see this term 'foo' 'bar' in many programming books, what is the
> etymology of this?
Repair (or Recognition). 'foo', 'bar' is just a play on the original
acronym - A joke forever obsorbed into common usage.
Randy.
Randy W. Sims Guest
-
Charles K. Clarkson #6
RE: Search Replace in multiple files
[email]chetak.sasalu@wipro.com[/email] <chetak.sasalu@wipro.com> wrote:
:
: I went through perldoc perlrun and saw the code.
:
: I thought it as a criminal waste of time to try and
: modify that code for my purpose, when I can ask you
: folks :-)
Perhaps I am misunderstanding you, but that sounds
to me like you would rather get us to do your work for
free instead of you acquiring the mental tools to do it
yourself. In the long run, that doesn't really help you,
does it?
Many of us are here to *help* you get past the
problems you are having with your code. We are not here
as a code writing resource. Again, perhaps I am
misreading your message. If so, my apologies.
If not, try to find answers on your own first.
Look at the documentation that comes with perl. Search
the internet with Google. We help a lot of folks here,
but we assume you have at least tried to solve things
yourself.
Take a look at the archives. Many of us that help
very rarely ask questions here. I, for one, have
learned how to answer most of my questions through
research and by "wasting my time" and *not* asking here
first. I'll bet a lot of programmers more experienced
then me do the same.
: PS: I see this term 'foo' 'bar' in many programming
: books, what is the etymology of this?
RFC 3092 - Etymology of "Foo":
[url]http://www.faqs.org/rfcs/rfc3092.html[/url]
BTW, this was the first link returned by Google.
You could have at least tried to find it on your own.
[url]http://www.google.com/search?q=foo+etymology[/url]
HTH,
Charles K. Clarkson
--
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328
Charles K. Clarkson Guest
-
Ned Cunningham #7
RE: Search Replace in multiple files
Wouldn't it be better to not waste your time with this kind of response???
Ned Cunningham
POS Systems Development
Monro Muffler Brake
200 Holleder Parkway
Rochester, NY 14615
(585) 647-6400 ext. 310
[email]ned.cunningham@monro.com[/email]
-----Original Message-----
From: Charles K. Clarkson [mailto:cclarkson@htcomp.net]
Sent: Friday, February 06, 2004 9:54 AM
To: [email]beginners@perl.org[/email]
Subject: RE: Search Replace in multiple files
[email]chetak.sasalu@wipro.com[/email] <chetak.sasalu@wipro.com> wrote:
:
: I went through perldoc perlrun and saw the code.
:
: I thought it as a criminal waste of time to try and
: modify that code for my purpose, when I can ask you
: folks :-)
Perhaps I am misunderstanding you, but that sounds
to me like you would rather get us to do your work for
free instead of you acquiring the mental tools to do it
yourself. In the long run, that doesn't really help you,
does it?
Many of us are here to *help* you get past the
problems you are having with your code. We are not here
as a code writing resource. Again, perhaps I am
misreading your message. If so, my apologies.
If not, try to find answers on your own first.
Look at the documentation that comes with perl. Search
the internet with Google. We help a lot of folks here,
but we assume you have at least tried to solve things
yourself.
Take a look at the archives. Many of us that help
very rarely ask questions here. I, for one, have
learned how to answer most of my questions through
research and by "wasting my time" and *not* asking here
first. I'll bet a lot of programmers more experienced
then me do the same.
: PS: I see this term 'foo' 'bar' in many programming
: books, what is the etymology of this?
RFC 3092 - Etymology of "Foo":
[url]http://www.faqs.org/rfcs/rfc3092.html[/url]
BTW, this was the first link returned by Google.
You could have at least tried to find it on your own.
[url]http://www.google.com/search?q=foo+etymology[/url]
HTH,
Charles K. Clarkson
--
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Ned Cunningham Guest
-
Nigel Peck - Mis Web Design #8
Net::SMTP Problem - Bad Protocol
I have a problem with Net::SMTP running a Win32 server (groan).
It works fine from the command line but when I try it through cgi I get:
Net::SMTP: Bad protocol 'tcp' at...
All help greatly appreciated :)
Cheers,
Nigel
Managing Director
MIS Web Design
[url]http://www.miswebdesign.com/[/url]
Administrator
AccessifyForum.com
[url]http://www.accessifyforum.com/[/url]
Nigel Peck - Mis Web Design Guest
-
John W. Krahn #9
Re: Search Replace in multiple files
Chetak Sasalu wrote:
Hello,>
> Hi,
use File::Find;> I have to search and replace 'foo' to 'bar' in all the files in a
> directory(has subdirectories).
> The files are about 40k in size.
>
> On the command line I would do it as,
> find ./mydir/ -type f -print | xargs perl -pi -e 's/foo/bar/'
>
> No backup of the original files required.I am brave.
>
> What is the most efficient way to implement this inside a perl program ?
> There are about 30 files to be processed.
> I went through perldoc perlrun and saw the code.
>
> I thought it as a criminal waste of time to try and modify that code for
> my purpose, when I can ask you folks :-)
local ( $^I, @ARGV ) = '';
find( { no_chdir => 1, wanted => sub { -f and push @ARGV, $_ } }, './mydir' );
s/foo/bar/g, print while <>;
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
R. Joseph Newton #10
Re: Search Replace in multiple files
Ned Cunningham wrote:
Hi Ned,> Wouldn't it be better to not waste your time with this kind of response???
>
> Ned Cunningham
> POS Systems Development
I'd say that it depends on whether you see the OP as a potential programmer. If
so, then no, it would not be better. If the OP takes a hint and takes another look
at his approach to learning, then this is time well spent. If not, the yes, this
would certainly be a gratuitous post. OTOH, it might still help the OP clarify his
purpose and direction.
Joseph
R. Joseph Newton Guest
-
Rob Dixon #11
Re: Search Replace in multiple files
John W. Krahn wrote:
Thanks John.>
> Chetak Sasalu wrote:>> >
> > Hi,
> Hello,
>>> > I have to search and replace 'foo' to 'bar' in all the files in a
> > directory(has subdirectories).
> > The files are about 40k in size.
> >
> > On the command line I would do it as,
> > find ./mydir/ -type f -print | xargs perl -pi -e 's/foo/bar/'
> >
> > No backup of the original files required.I am brave.
> >
> > What is the most efficient way to implement this inside a perl program ?
> > There are about 30 files to be processed.
> > I went through perldoc perlrun and saw the code.
> >
> > I thought it as a criminal waste of time to try and modify that code for
> > my purpose, when I can ask you folks :-)
> use File::Find;
> local ( $^I, @ARGV ) = '';
> find( { no_chdir => 1, wanted => sub { -f and push @ARGV, $_ } }, './mydir' );
> s/foo/bar/g, print while <>;
I missed the 'has subdirectories'.
Rob
Rob Dixon Guest
-
Rob Dixon #12
Re: Search Replace in multiple files
Jan Eden wrote:
Hi Jan.>
> Rob Dixon wrote:
>>> >John W. Krahn wrote:>> >> >> use File::Find;
> >> local ( $^I, @ARGV ) = '';
> >> find( { no_chdir => 1, wanted => sub { -f and push @ARGV, $_ } }, './mydir' );
> >> s/foo/bar/g, print while <>;
> >Thanks John.
> >
> >I missed the 'has subdirectories'.
> >
> I did not and came up with a similar (but simpler) idea:
>
> use File::Find;
> local $^I = '';
> find( { sub { push @ARGV, $_ if -f }, 'dir' );
> while (<>) {
> s/foo/bar/g;
> print;
> }
>
> I forgot to empty @ARGV, and I see why it's necessary. But I do not understand John's
> anonymous subroutine as find's first argument. Could someone explain it in a little
> more detail?
If you think about it, you'll see that it's an anonymous hash
{ .. }
not an anonymous subroutine
sub { .. }
In the simple case where you don't need any special options you can pass a code reference
as the first parameter to 'find' which is used as the 'wanted' subroutine. John wanted
'no_chdir', so he had to use the full form. This option prevents file() from doing a chdir
to the the current directory ($File::Find::dir) before it calls the 'wanted' routine.
I would guess two reasons why he did it this way: firstly it should be slightly faster
than the default, which is to change directories; secondly it leaves $_ set to the
same value as $File::Find::name instead of just the file's basename within the directory
so the 'wanted' routine comes out a lot neater.
HTH,
Rob
Rob Dixon Guest
-
Jan Eden #13
Re: Search Replace in multiple files
Rob Dixon wrote:
I will never learn: After your hint to a "full form", I checked perldoc File::Find and learned about the options. So far, I had only read the Camel's short description of File::Find.>Jan Eden wrote:>>>
>>Rob Dixon wrote:
>>>>>>>John W. Krahn wrote:>>>>>>use File::Find; local ( $^I, @ARGV ) = ''; find( { no_chdir => 1,
>>>>wanted => sub { -f and push @ARGV, $_ } }, './mydir' );
>>>>s/foo/bar/g, print while <>;
>>>
>>>Thanks John.
>>>
>>>I missed the 'has subdirectories'.
>>>
>>I did not and came up with a similar (but simpler) idea:
>>
>>use File::Find; local $^I = ''; find( { sub { push @ARGV, $_ if -f
>>}, 'dir' ); while (<>) { s/foo/bar/g; print;
>>}
>>
>>I forgot to empty @ARGV, and I see why it's necessary. But I do not
>>understand John's anonymous subroutine as find's first argument.
>>Could someone explain it in a little more detail?
>Hi Jan.
>
>If you think about it, you'll see that it's an anonymous hash
>
>{ .. }
>
>not an anonymous subroutine
>
>sub { .. }
>
>In the simple case where you don't need any special options you can
>pass a code reference as the first parameter to 'find' which is used
>as the 'wanted' subroutine. John wanted 'no_chdir', so he had to use
>the full form. This option prevents file() from doing a chdir to the
>the current directory ($File::Find::dir) before it calls the 'wanted'
>routine.
Thank you,
Jan
--
There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. - Jeremy S. Anderson
Jan Eden Guest
-
Chetak Sasalu #14
RE: Search Replace in multiple files
Hello,
I came back to office after an extended weekend and realized that my
words might have irked some of you.
But let me explain.
I agree, acquiring the 'mental tools' to do it myself is always the best
way. I had tried out a few sample scripts
myself (most of them failed). After spending a few hours on it my
morals got weak and I could not resist the temptation to post to this
group.
I am always surprised by innovative solutions by the gurus in this
group.
Like the way to initialize all the array elements to zero - 0 x @array
.. These kind of solutions make me read the perldocs.
I do read the archives whenever I find time, Even when I have not
posted any questions. It is a pleasure to read some of your mails. I am
wary of answering to any queries though. Invariably, I see a better
solution in response then what I had in mind. I am learning and I will
be answering like "anything" soon ;)
Thanks, Charles K Clarkson. I understand your concerns. I will google
and cpan and try hard to find the answer myself before I post to this
group.
Larry wall says that laziness is a virtue, but my laziness bordered on
brazenness.
Cheers,
Chetak
[email]chetak.sasalu@wipro.com[/email] <chetak.sasalu@wipro.com> wrote:
:
: I went through perldoc perlrun and saw the code.
:
: I thought it as a criminal waste of time to try and
: modify that code for my purpose, when I can ask you
: folks :-)
Perhaps I am misunderstanding you, but that sounds
to me like you would rather get us to do your work for
free instead of you acquiring the mental tools to do it yourself. In the
long run, that doesn't really help you, does it?
Many of us are here to *help* you get past the
problems you are having with your code. We are not here
as a code writing resource. Again, perhaps I am
misreading your message. If so, my apologies.
If not, try to find answers on your own first.
Look at the documentation that comes with perl. Search
the internet with Google. We help a lot of folks here,
but we assume you have at least tried to solve things
yourself.
Take a look at the archives. Many of us that help
very rarely ask questions here. I, for one, have
learned how to answer most of my questions through
research and by "wasting my time" and *not* asking here
first. I'll bet a lot of programmers more experienced
then me do the same.
: PS: I see this term 'foo' 'bar' in many programming
: books, what is the etymology of this?
RFC 3092 - Etymology of "Foo":
[url]http://www.faqs.org/rfcs/rfc3092.html[/url]
BTW, this was the first link returned by Google.
You could have at least tried to find it on your own.
[url]http://www.google.com/search?q=foo+etymology[/url]
HTH,
Charles K. Clarkson
--
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Confidentiality Notice
The information contained in this electronic message and any attachments tothis message are intended
for the exclusive use of the addressee(s) and may contain confidential orprivileged information. If
you are not the intended recipient, please notify the sender at Wipro [email]orMailadmin@wipro.com[/email] immediately
and destroy all copies of this message and any attachments.
Chetak Sasalu Guest
-
Rob Dixon #15
Re: Search Replace in multiple files
Chetak Sasalu wrote:
'Irked' is a good word. I'll happily forgive just because of that, plus the fact>
> I came back to office after an extended weekend and realized that my words
> might have irked some of you.
that I know that I also can be irksome.
Rob
Rob Dixon Guest



Reply With Quote

