in reply to (jeffa) 3Re: Searching for variable then blank lines
in thread Searching for variable then blank lines

SUBJECT WAS: Searching for variable then blank lines

Thanks to everyone who has helped me on this. I must be dense. :-\ Now I am getting my script to read in my file and go down to the beginning of the line starting with ARCSERVE.NLM, and it gives me *every* line after that. My script is not not exiting when it finds the first 2 returns after ARCSERVE.NLM for the first time. Below is my script and a sample of the output file.

use strict; use File::Basename; ##******** Variables ********## my $filepath = 'c:\a\netware'; my $dump = 'Netware_hw.txt'; my $arc = 'ARCSERVE.NLM'; my $serverfile = 'dallas.txt'; open (SERVER, "<$serverfile") or die "Can't open the file\n"; open (OUTPUT, ">>$filepath\\$dump"); my @lines= <SERVER>; my ($s, $b); foreach my $line (@lines) { if ($line =~ /^\n$/) { $b++; last if (($b == 2) && $s); } else { $b = 0; } if ($line =~ /$arc/) { $s = 'true'; } if ($s) { print OUTPUT $line; } } close (OUTPUT); close (SERVER); =========== OUTPUT FILE =========== ARCSERVE.NLM Version: 1 Date: 10/22/2000 ID: 000 Parent ID: x Type: UNKNOWN Description: ARCserve Flags: ASDB.NLM Version: 2 Date: 12/8/2000 ASPIBD.NLM Version: 4 Date: 12/15/2000 ID: 0
This is just an example. My file was much larger than this. I was not wanting the information starting with ASDB.NLM to the end of the file. What do I need to do to make this happen?

Thx again!

Replies are listed 'Best First'.
(jeffa) Re: Exiting when parsing files
by jeffa (Bishop) on Mar 15, 2002 at 15:05 UTC
    Hi rscott212. Sorry for the delay ... but you don't want to hear my problems ... ;)

    Ok, i changed this line:

    # match a line that only contains one new line if ($line =~ /^\n$/) {
    To this:
    # match a line that only contains one or more whitespace if ($line =~ /^\s+$/) {
    And it worked ... there was one extra newline at the end of the extracted data, but i'll get to that in a moment. (And for the record, one of your 'blank lines' had a single space in it.)

    Now, some thoughts on your thoughts. First, please learn how to indent properly. When i first glanced at your code, i really believed that the last two if blocks were OUTSIDE the foreach loop. Please, for the sanity of those trying to help, use good formatting:

    foreach (@blah) { if ($foo) { # foo stuff } else { # other stuff } }
    That makes all the difference in the world. Second, the integer 1 (one) makes a great true value. Instead of assigning a boolean flag the scalar value 'TRUE', just use 1 (one).

    Last, you have a lot of unecessary code - here is a slight improvement for you to ponder:

    # open filehandles ... my ($s, $b) = (0,0); my $arc = 'ARCSERVE.NLM'; my @lines = <DATA>; foreach my $line (@lines) { if ($line =~ /^\s+$/ and $s) { $b++; last if $b == 2; } $s = 1 if $line =~ /$arc/; print OUTPUT $line if $s and $line !~ /^\s+$/; }
    This produces (using your data file) the following output:
    ARCSERVE.NLM
      Version:            1
      Date:                10/22/2000
      ID:                000
      Parent ID:            x
      Type:                UNKNOWN
      Description:            ARCserve
      Flags: 
    
    Hope this helps. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)