Opening a file twice and having an if loop

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

Gunnar Hjalmarsson

Slain said:
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.

What does it mean when you say that "the script kind of gets messed up"?
Can some one tell me what mistake I am making in the script below?

There is a lot to say about your script, but since you chose to post
here, what comes to mind first is to call your attention to the posting
guidelines for this Usenet group:

http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Please study and follow those guidelines, and if doing so doesn't help
you solve the problem, post here in accordance with the guidelines.
 
M

Mirco Wahab

Slain said:
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?

This is nearly impossible, because the script
contains too much problems and non-working
approaches.

As far as I can see, you want to replace
the string 'Generic_Text' in your files
by the actual <title>. If so, (under
unixish OS), the line:

perl -i.old -0777 -pe '($t)=/<title>(.+?)<\/title>/ and s/Generic_Text/$t/' index.html

would suffice.

But you probably want to say something
first what's your real intention.

Regards

M.
 
S

Slain

This is nearly impossible, because the script
contains too much problems and non-working
approaches.

As far as I can see, you want to replace
the string 'Generic_Text' in your files
by the actual <title>. If so, (under
unixish OS), the line:

perl -i.old -0777 -pe '($t)=/<title>(.+?)<\/title>/ and s/Generic_Text/$t/' index.html

would suffice.

But you probably want to say something
first what's your real intention.

Regards

M.

Thanks!!!! You are correct, I am trying to replace 'Generic_Text' with
the text which is between <title>String2<title>

Since this String2 is different in different files, my aim is to read
that String 2 and replace Generic_Text with it. I need to run this on
windows. So is there a better way to just read that particular line?
 
M

Mirco Wahab

Slain said:
Since this String2 is different in different files, my aim is to read
that String 2 and replace Generic_Text with it. I need to run this on
windows. So is there a better way to just read that particular line?

On Windows, you have to take into account:
- different command string delimiter
- no wildcard shell expansion

Therefore, the construct to change your text under windows,
for all .html per directory, would read sth. like ...

FOR %i in (*.html) DO perl -i.old -0777 -pe "($t)=/<title>(.+?)<\/title>/s and s/Generic_Text/$t/" %i

if I'm not totally wrong here.

Regards

M.
 
S

Slain

On Windows, you have to take into account:
- different command string delimiter
- no wildcard shell expansion

Therefore, the construct to change your text under windows,
for all .html per directory, would read sth. like ...

FOR %i in (*.html) DO perl -i.old -0777 -pe "($t)=/<title>(.+?)<\/title>/s and s/Generic_Text/$t/" %i

if I'm not totally wrong here.

Regards

M.

That did it, Thanks a lot!!!
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top