Opening a file twice and having an if loop

Discussion in 'Perl Misc' started by Slain, Jun 9, 2007.

  1. Slain

    Slain Guest

    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);
    Slain, Jun 9, 2007
    #1
    1. Advertising

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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 9, 2007
    #2
    1. Advertising

  3. Slain

    Mirco Wahab Guest

    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.
    Mirco Wahab, Jun 9, 2007
    #3
  4. Slain

    Slain Guest

    On Jun 9, 3:57 am, Mirco Wahab <> 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?
    Slain, Jun 9, 2007
    #4
  5. Slain

    Mirco Wahab Guest

    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.
    Mirco Wahab, Jun 9, 2007
    #5
  6. Slain

    Slain Guest

    On Jun 9, 9:47 am, Mirco Wahab <> 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!!!
    Slain, Jun 9, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Barkster

    File Dialog opening twice?

    Barkster, Jun 3, 2006, in forum: Java
    Replies:
    0
    Views:
    595
    Barkster
    Jun 3, 2006
  2. Kiuhnm

    twice(twice(x))

    Kiuhnm, Apr 1, 2006, in forum: C++
    Replies:
    2
    Views:
    386
    Kiuhnm
    Apr 1, 2006
  3. David C

    Browser opening file twice

    David C, Apr 3, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    321
    David C
    Apr 4, 2008
  4. Bigmac Turdsplash

    loop, insdide do loop. do twice?

    Bigmac Turdsplash, Feb 3, 2011, in forum: Ruby
    Replies:
    1
    Views:
    111
    Robert Klemme
    Feb 3, 2011
  5. Isaac Won
    Replies:
    9
    Views:
    355
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page