File search & replace

Discussion in 'Perl Misc' started by axl.rajesh@gmail.com, Feb 20, 2006.

  1. Guest

    Hi,

    I have a requirement, where I need to do recursive search and replace
    for files of around 50,000 lines of code. currently I wrote the
    following perl script and using it for searching the file and replace.


    opendir (DIR, "c:\\temp") or die "can not open directory: $! \n";
    for my $x (readdir(DIR)) {

    next if ($x eq '.' or $x eq '..');
    chdir ("c:\\temp") or die "unable to move to the target directory :
    $! \n";

    open (FH, "+< $x") or die "can not open file: $! \n";
    unlink "$x.bak" if (-e "$x.bak");
    open (FH1, ">> $x.bak") or die "can not open file : $! \n";

    while (<FH>) {

    s/search/replace/g ;
    print FH1 $_;

    }

    close(FH);
    close(FH1);
    }

    In the current script I am creating a new file, while keeping the old
    backup.

    can any one suggest me to edit the files directly.
    I have also tried using $^I = ' ';
    But that trial was not succesful.

    2) can i edit the file directly when opened in "read/write" as shown
    below.
    open (FH , "+< file.txt);

    Thanks,
    Rajesh.
    , Feb 20, 2006
    #1
    1. Advertising

  2. tuser Guest

    wrote:
    > Hi,
    >
    > I have a requirement, where I need to do recursive search and replace
    > for files of around 50,000 lines of code. currently I wrote the
    > following perl script and using it for searching the file and replace.
    >
    >
    > opendir (DIR, "c:\\temp") or die "can not open directory: $! \n";


    it's better to use one forward slash instead of two back slashes:

    opendir (DIR, "c:/temp") or die "can not open directory: $! \n";

    [ snip ]

    > In the current script I am creating a new file, while keeping the old
    > backup.
    >
    > can any one suggest me to edit the files directly.


    perldoc perlrun
    (see option "-i")

    ....or even...
    perldoc -q -i
    tuser, Feb 20, 2006
    #2
    1. Advertising

  3. wrote:
    >
    > I have a requirement, where I need to do recursive search and replace
    > for files of around 50,000 lines of code. currently I wrote the
    > following perl script and using it for searching the file and replace.
    >
    >
    > opendir (DIR, "c:\\temp") or die "can not open directory: $! \n";
    > for my $x (readdir(DIR)) {
    >
    > next if ($x eq '.' or $x eq '..');
    > chdir ("c:\\temp") or die "unable to move to the target directory :
    > $! \n";
    >
    > open (FH, "+< $x") or die "can not open file: $! \n";
    > unlink "$x.bak" if (-e "$x.bak");
    > open (FH1, ">> $x.bak") or die "can not open file : $! \n";
    >
    > while (<FH>) {
    >
    > s/search/replace/g ;
    > print FH1 $_;
    >
    > }
    >
    > close(FH);
    > close(FH1);
    > }
    >
    > In the current script I am creating a new file, while keeping the old
    > backup.
    >
    > can any one suggest me to edit the files directly.
    > I have also tried using $^I = ' ';
    > But that trial was not succesful.


    You should be able to do it something like this (UNTESTED):

    ( $^I, @ARGV ) = ( '.bak', glob 'c:/temp/*' );
    while ( <> ) {
    s/search/replace/g;
    print;
    }


    > 2) can i edit the file directly when opened in "read/write" as shown
    > below.
    > open (FH , "+< file.txt);


    You can modify the file like that but you cannot change the size of the file.



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Feb 20, 2006
    #3
  4. Michael Greb Guest

    In article <>,
    "tuser" <> wrote:

    > wrote:
    > > Hi,
    > >
    > > I have a requirement, where I need to do recursive search and replace
    > > for files of around 50,000 lines of code. currently I wrote the
    > > following perl script and using it for searching the file and replace.
    > >
    > >
    > > opendir (DIR, "c:\\temp") or die "can not open directory: $! \n";

    >
    > it's better to use one forward slash instead of two back slashes:
    >
    > opendir (DIR, "c:/temp") or die "can not open directory: $! \n";


    And/Or using single quotes since no processing is necessary:
    opendir (DIR, 'c:/temp') or die "can not open directory: $! \n";

    <snip>
    Michael Greb, Feb 20, 2006
    #4
  5. writes:

    > I have a requirement, where I need to do recursive search and
    > replace for files of around 50,000 lines of code. currently I wrote
    > the following perl script and using it for searching the file and
    > replace.


    I don't know how easily this translates to Windows, but on Unix, it's
    very simple:

    perl -i -p -e 's/search/replace/g' *

    That assumes, like your sample code does, that the search pattern
    never spans multiple lines, and that all the files are in one
    directory. If you aren't extremely sure that your search/replace will
    do what you expect, you can make a backup of each file:

    perl -i.bak -p -e 's/search/replace/g' *

    and then easily delete all the *.bak files after you check the changed
    ones to make sure you didn't screw them up.


    --
    Aaron --
    http://360.yahoo.com/aaron_baugher
    Aaron Baugher, Feb 20, 2006
    #5
  6. wrote:
    > I have a requirement, where I need to do recursive search and replace
    > for files of around 50,000 lines of code. currently I wrote the
    > following perl script and using it for searching the file and replace.
    >
    >
    > opendir (DIR, "c:\\temp") or die "can not open directory: $! \n";
    > for my $x (readdir(DIR))


    Any specific reason why you are not using File::Find?

    jue
    Jürgen Exner, Feb 20, 2006
    #6
    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. Dorsa
    Replies:
    0
    Views:
    445
    Dorsa
    Dec 23, 2003
  2. sebb
    Replies:
    2
    Views:
    321
    Miki Tebeka
    Jan 12, 2004
  3. Ryan Krauss
    Replies:
    1
    Views:
    371
    James Stroud
    Jun 20, 2006
  4. techie2go
    Replies:
    3
    Views:
    256
    techie2go
    Sep 10, 2006
  5. Abby Lee
    Replies:
    5
    Views:
    366
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page