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