File search & replace

A

axl.rajesh

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

tuser

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
 
J

John W. Krahn

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
 
M

Michael Greb

tuser said:
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>
 
A

Aaron Baugher

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

Jürgen Exner

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
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top