how to replace string for all files under a dir?

G

Guagua

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!
 
J

Jürgen Exner

Guagua said:
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
 
P

Paul Lalli

Guagua said:
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
 
G

Guagua

Paul said:
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...
 
J

John W. Krahn

Guagua said:
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
 
J

Joe Smith

Guagua said:
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
 
D

Damian James

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

John W. Krahn

Damian said:
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
 
D

Damian James

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
 
J

John W. Krahn

Damian said:
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.

Ok, but I was using File::Finder so a closer translation would be:

perl -i.bak -pe's/AAA/BBB/g' `find directory -type f -name '*.txt'`


:)

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top