how to replace string for all files under a dir?

Discussion in 'Perl Misc' started by Guagua, Aug 16, 2005.

  1. Guagua

    Guagua Guest

    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!
     
    Guagua, Aug 16, 2005
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Aug 16, 2005
    #2
    1. Advertising

  3. Guagua

    Guagua Guest

    Guagua, Aug 16, 2005
    #3
  4. Guagua

    Paul Lalli Guest

    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
     
    Paul Lalli, Aug 16, 2005
    #4
  5. Guagua

    Guagua Guest

    > 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...
     
    Guagua, Aug 16, 2005
    #5
  6. 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
     
    John W. Krahn, Aug 16, 2005
    #6
  7. Guagua

    Joe Smith Guest

    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
     
    Joe Smith, Aug 16, 2005
    #7
  8. Guagua

    Damian James Guest

    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
     
    Damian James, Aug 16, 2005
    #8
  9. 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
     
    John W. Krahn, Aug 16, 2005
    #9
  10. Guagua

    Damian James Guest

    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
     
    Damian James, Aug 16, 2005
    #10
  11. Damian James wrote:
    > 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.


    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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Aug 16, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UnVkeQ==?=

    Sub Dir, Virtual dir, what do I use?

    =?Utf-8?B?UnVkeQ==?=, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    434
    =?Utf-8?B?UnVkeQ==?=
    Jun 12, 2005
  2. Xah Lee
    Replies:
    1
    Views:
    1,385
    YYusenet
    Jan 31, 2005
  3. Matthew Denner
    Replies:
    1
    Views:
    188
  4. Kga Agk
    Replies:
    2
    Views:
    153
    Kga Agk
    Jun 29, 2009
  5. Xah Lee
    Replies:
    0
    Views:
    350
    Xah Lee
    Jan 31, 2005
Loading...

Share This Page