Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to replace string for all files under a dir?

Reply
Thread Tools

how to replace string for all files under a dir?

 
 
Guagua
Guest
Posts: n/a
 
      08-16-2005
I would like to replace all strings as "AAA" to strings as "BBB" for
all text files under a diretory.
I'm new to Perl, but I know it's good at this kind of task.
Could someone show me how to do it?
Thanks a lot!

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      08-16-2005
Guagua wrote:
> I would like to replace all strings as "AAA" to strings as "BBB" for
> all text files under a diretory.
> I'm new to Perl, but I know it's good at this kind of task.
> Could someone show me how to do it?


perldoc File::Find
perldoc -q change: "How do I change one line in a file/delete a line in a
file/insert a line in the middle of a file/append to the beginning of a
file?"

jue


 
Reply With Quote
 
 
 
 
Guagua
Guest
Posts: n/a
 
      08-16-2005
Thank you for the hints.

Actually I just found one Perl script online doing exactly what I need
to do.
Here is the pointer:
http://www.neilgunton.com/utils/deep/
Enjoy!

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-16-2005
Guagua wrote:
> Thank you for the hints.


Thank who? For what hints? Please follow standard Usenet traditions
and quote some context when you reply.

> Actually I just found one Perl script online doing exactly what I need
> to do.
> Here is the pointer:
> http://www.neilgunton.com/utils/deep/


Your original post asked for pointers to show you how to solve your
problem. Using a ready-made solution teaches you nothing.

I recommend you at least attempt to read the documentation.

Paul Lalli

 
Reply With Quote
 
Guagua
Guest
Posts: n/a
 
      08-16-2005
> Paul Lalli wrote:
> Thank who? For what hints? Please follow standard Usenet traditions
> and quote some context when you reply.


Yes, Thanks.

>
> Your original post asked for pointers to show you how to solve your
> problem. Using a ready-made solution teaches you nothing.
>
> I recommend you at least attempt to read the documentation.


I do want to know how to solve the problem, instead of just getting the
solution. However, it's still hard for me (who have very limited
knowledge of Perl right now) to write out the code even after reading
the document. So I figure that maybe it's better to get a solution then
understand how it works...

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-16-2005
Guagua wrote:
> I would like to replace all strings as "AAA" to strings as "BBB" for
> all text files under a diretory.
> I'm new to Perl, but I know it's good at this kind of task.
> Could someone show me how to do it?



#!/usr/bin/perl
use warnings;
use strict;

use File::Finder;

@ARGV = grep -T, File::Finder->type( 'f' )->in( 'directory' );

$^I = '.bak';

while ( <> ) {
s/AAA/BBB/g;
print;
}

__END__


If you just want all files with the extention '.txt' then use the line:

@ARGV = File::Finder->type( 'f' )->name( '*.txt' )->in( 'directory' );



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      08-16-2005
Guagua wrote:
> I would like to replace all strings as "AAA" to strings as "BBB" for
> all text files under a diretory.


If all the files are in one directory (and not subdirectories),
then the solution is trivial and does not need to use File::Find.

linux% perl -pi -e 's/AAA/BBB/g' directory/*.txt

That's all it takes.
-Joe
 
Reply With Quote
 
Damian James
Guest
Posts: n/a
 
      08-16-2005
On Tue, 16 Aug 2005 06:10:59 GMT, John W. Krahn said:
> Guagua wrote:
>> I would like to replace all strings as "AAA" to strings as "BBB" for
>> all text files under a diretory.
>> I'm new to Perl, but I know it's good at this kind of task.
>> Could someone show me how to do it?

> ...
> If you just want all files with the extention '.txt' then use the line:
>
> @ARGV = File::Finder->type( 'f' )->name( '*.txt' )->in( 'directory' );
>


Though in that case,

perl -i.bak -pale 's/AAA/BBB/g' /path/to/directory/*.txt

would do the trick. But that's probably what you meant .

--Damian
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-16-2005
Damian James wrote:
> On Tue, 16 Aug 2005 06:10:59 GMT, John W. Krahn said:
>>Guagua wrote:
>>>I would like to replace all strings as "AAA" to strings as "BBB" for
>>>all text files under a diretory.
>>>I'm new to Perl, but I know it's good at this kind of task.
>>>Could someone show me how to do it?

>>...
>>If you just want all files with the extention '.txt' then use the line:
>>
>>@ARGV = File::Finder->type( 'f' )->name( '*.txt' )->in( 'directory' );

>
> Though in that case,
>
> perl -i.bak -pale 's/AAA/BBB/g' /path/to/directory/*.txt
>
> would do the trick. But that's probably what you meant .


I think you meant to reply to Joe Smith's post?

By the way, why are you using the -a switch which splits the line into
the @F array when you are not using the @F array, and why are you using
the -l switch which removes the newline and then adds it back in when
the line is printed?


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Damian James
Guest
Posts: n/a
 
      08-16-2005
On Tue, 16 Aug 2005 09:26:49 GMT, John W. Krahn said:
>> perl -i.bak -pale 's/AAA/BBB/g' /path/to/directory/*.txt

>
> I think you meant to reply to Joe Smith's post?


No, though certainly I was not meaning to correct yours, just
making relations that I felt the OP would not have recognised
otherwise, but might nonetheless have been interested to see.

Specifically, you were making use of the various facilities that
Perl includes for the sake of one liners, my intention was to
translate that into the nearest equivalent one liner.

> By the way, why are you using the -a switch which splits the line into
> the @F array when you are not using the @F array, and why are you using
> the -l switch which removes the newline and then adds it back in when
> the line is printed?


Gah.

Well, I guess -l does the right thing, albeit pointlessly. -a is quite
useless in this context, and I can only put it down to habit. I seem to
always read logfiles with a comand that starts "perl -lane ..." and in
this context I am usually doing something useful with @F and the removal
of the newline if not important but adding it back is.

*running and hiding*

--Damian
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
how to make replace function replace globally in a string V S Rawat Javascript 5 07-03-2007 08:02 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments