Search Replace in multiple files

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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... ...
    5. 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...
  3. #2

    Default Re: Search Replace in multiple files

    [email]chetak.sasalu@wipro.com[/email] wrote:
    >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.
    >
    I don't know if this is the most efficient way, but it works:

    #!/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

  4. #3

    Default 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

  5. #4

    Default Re: Search Replace in multiple files

    Chetak Sasalu wrote:
    >
    > 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 strict;
    use warnings;

    local @ARGV = grep -f, <./mydir/*>;

    $^I = '';

    while (<>) {
    s/foo/bar/g;
    print;
    }

    Rob


    Rob Dixon Guest

  6. #5

    Default Re: Search Replace in multiple files

    On 02/06/04 07:45, [email]chetak.sasalu@wipro.com[/email] wrote:
    > PS: I see this term 'foo' 'bar' in many programming books, what is the
    > etymology of this?
    It is derived from a technical acronym FUBAR - F@#!ed Up Beyond All
    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

  7. #6

    Default 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

  8. #7

    Default 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

  9. #8

    Default 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

  10. #9

    Default Re: Search Replace in multiple files

    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 <>;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  11. #10

    Default Re: Search Replace in multiple files

    Ned Cunningham wrote:
    > Wouldn't it be better to not waste your time with this kind of response???
    >
    > Ned Cunningham
    > POS Systems Development
    Hi Ned,

    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

  12. #11

    Default Re: Search Replace in multiple files

    John W. Krahn wrote:
    >
    > 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 <>;
    Thanks John.

    I missed the 'has subdirectories'.

    Rob


    Rob Dixon Guest

  13. #12

    Default Re: Search Replace in multiple files

    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.

    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

  14. #13

    Default Re: Search Replace in multiple files


    Rob Dixon wrote:
    >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.
    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.

    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

  15. #14

    Default 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

  16. #15

    Default Re: Search Replace in multiple files

    Chetak Sasalu wrote:
    >
    > I came back to office after an extended weekend and realized that my words
    > might have irked some of you.
    'Irked' is a good word. I'll happily forgive just because of that, plus the fact
    that I know that I also can be irksome.

    Rob


    Rob Dixon Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139