S
Slain
I am writing this perl script, whose aim is to run through a file,
look for a particular string, if found, store it in a variable -
$str1. And then replace another string - $Str2 with the string $Str1.
In a previous version of the script, I was just swapping out one text
for another and it worked fine. If it found it swapped it, else it
just closed the file.
With the addition done, to look for a particular string first and then
swap if needed, the script kind of gets messed up, when the text I am
looking for is not present in the file. Since I am using this script
to run through multiple files, there will be files without the string
I am looking for in which case I do not want it to do anything. Can
some one tell me what mistake I am making in the script below?
Thanks
foreach $filename (@filelist) {
print " P: $filename\n";
$filename1 = $filename;
open (file, "$filename1") || die("Error Reading File: $filename $!");
{
my $target = "<title>";
while(<file>)
{
my($line) = $_;
chomp($line);
#print "the target is $target \n";
if(/$target/) {
print "found target on line $. \n";
($a,$b,$c)=split(/</,$line);
($d,$e) = split(/>/,$b);
@find3= "Generic_Text";
@replace3 = "$e";
#$replace3 = "media module";
print "the three splits are $a, $b, $c \n";
print "the splits are $d, $e \n";
}
}
}
close (file)|| die("Error Closing File: $filename $!");
# retrieve complete file
open (IN, "$filename") || die("Error Reading File: $filename $!");
{
undef $/;
$infile = <IN>;
}
close (IN) || die("Error Closing File: $filename $!");
$infile =~ s/@find3/@replace3/g;
# write complete file
open (PROD, ">$filename") || die("Error Writing to File:
$filename $!");
print PROD $infile;
close (PROD) || die("Error Closing File: $filename $!");
}
print "\nFinished.\n";
exit(0);
look for a particular string, if found, store it in a variable -
$str1. And then replace another string - $Str2 with the string $Str1.
In a previous version of the script, I was just swapping out one text
for another and it worked fine. If it found it swapped it, else it
just closed the file.
With the addition done, to look for a particular string first and then
swap if needed, the script kind of gets messed up, when the text I am
looking for is not present in the file. Since I am using this script
to run through multiple files, there will be files without the string
I am looking for in which case I do not want it to do anything. Can
some one tell me what mistake I am making in the script below?
Thanks
foreach $filename (@filelist) {
print " P: $filename\n";
$filename1 = $filename;
open (file, "$filename1") || die("Error Reading File: $filename $!");
{
my $target = "<title>";
while(<file>)
{
my($line) = $_;
chomp($line);
#print "the target is $target \n";
if(/$target/) {
print "found target on line $. \n";
($a,$b,$c)=split(/</,$line);
($d,$e) = split(/>/,$b);
@find3= "Generic_Text";
@replace3 = "$e";
#$replace3 = "media module";
print "the three splits are $a, $b, $c \n";
print "the splits are $d, $e \n";
}
}
}
close (file)|| die("Error Closing File: $filename $!");
# retrieve complete file
open (IN, "$filename") || die("Error Reading File: $filename $!");
{
undef $/;
$infile = <IN>;
}
close (IN) || die("Error Closing File: $filename $!");
$infile =~ s/@find3/@replace3/g;
# write complete file
open (PROD, ">$filename") || die("Error Writing to File:
$filename $!");
print PROD $infile;
close (PROD) || die("Error Closing File: $filename $!");
}
print "\nFinished.\n";
exit(0);