Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Opening a file twice and having an if loop (http://www.velocityreviews.com/forums/t903403-opening-a-file-twice-and-having-an-if-loop.html)

Slain 06-09-2007 02:23 AM

Opening a file twice and having an if loop
 
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);


Gunnar Hjalmarsson 06-09-2007 04:12 AM

Re: Opening a file twice and having an if loop
 
Slain wrote:
> 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/clp...uidelines.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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Mirco Wahab 06-09-2007 07:57 AM

Re: Opening a file twice and having an if loop
 
Slain wrote:
> 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.

Slain 06-09-2007 12:25 PM

Re: Opening a file twice and having an if loop
 
On Jun 9, 3:57 am, Mirco Wahab <wahab-m...@gmx.de> wrote:
> Slain wrote:
> > 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.


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?



Mirco Wahab 06-09-2007 01:47 PM

Re: Opening a file twice and having an if loop
 
Slain wrote:
> 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.

Slain 06-09-2007 05:10 PM

Re: Opening a file twice and having an if loop
 
On Jun 9, 9:47 am, Mirco Wahab <wahab-m...@gmx.de> wrote:
> Slain wrote:
> > 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.


That did it, Thanks a lot!!!



All times are GMT. The time now is 03:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57