yskmonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi experts,

I have 2 simple problems:

I am searching for few lines randomly from my outfile.html and placing them in an final.txt file.

here is my code:

open OUTFILE, "<$outfile"; open FINAL, ">$final"; while (<OUTFILE>) { print FINAL if /(my first line)/; print FINAL if /(my second line)/; print FINAL if /(my third line)/;

here is my output

line1<br> line2<br> line3<br>

question 1

I would like to have a blank line in between line 2 and 3. where can I place the "\n" in my code so the outout will be like below?

line 1 line 2 line 3

question 2:

I am searching for few lines randomly from my outfile.html and placing them in an final.txt file.

I was successful till that point but my outfile.html has "br" at the end of each line as shown below.

line1<br> line2<br> line3<br>

how can I remove that?

Please Advise Thanks in advance!

Replies are listed 'Best First'.
Re: simple question on next line parameter
by ikegami (Patriarch) on May 19, 2009 at 03:47 UTC

    I would like to have a blank line in between line 2 and 3. where can I place the "\n" in my code so the outout will be like below?

    If you wish to continue assuming each match will occur only once and in order, you could do

    while (<OUTFILE>) { print FINAL $_ if /(my first line)/; print FINAL "$_\n" if /(my second line)/; print FINAL $_ if /(my third line)/; }

    I was successful till that point but my outfile.html has "br" at the end of each line as shown below.

    s/<br>$//;

      Thanks for your replies. I could figure out how to include new line but I am still stuck with how to remove the  <br> at the end of each line

      ikegami: you mentioned in the previous post to include

      s/<br>$//;
      where do I include that in my code? Please Advise experts!! Thanks again
        Wherever you want to remove the trailing <br>.

        It operates on $_ unless you bind another variable (using =~) to the s/// operator.

Re: simple question on next line parameter
by Polyglot (Chaplain) on May 19, 2009 at 03:44 UTC
    Why not just do it this way?
    open OUTFILE, "<$outfile"; my @OUT = <OUTFILE>; open FINAL, ">$final"; my $line; foreach $line(@OUT) { if ($line=~/(my first line)<br>/) {print FINAL "$1\n"} if ($line=~/(my second line)<br>/) {print FINAL "$1\n\n"} if ($line=~/(my third line)<br>/) {print FINAL "$1\n"} }

    Blessings,

    ~Polyglot~

      ...or even
      open OUTFILE, "<$outfile"; open FINAL, ">$final"; foreach my $line (<OUTFILE>) { print FINAL "$1\n" if $line =~ /(my first line|my second line|my third line)<br>/); }
      A user level that continues to overstate my experience :-))