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

I am attempting to print some info from a flat text file.
I was able to merge text that had been wrapped but now
need to print only the lines with complete value. here is an example of what is in the file

Switch name|Up|VCC||||||||||||||| RTC2||50.1|13|1|376|510|UBR|7|2|65|65|10|384|384|0|3|Off RTC1||50.2|3|2|23|191/3|UBR|7|2|65|65|10|38|38|40665|3|Off Switch name|Up|VCC||||||||||||||| RTC2||50.1|9|1|14|201|UBR|7|2|65|65|10|64|64|320|3|On RTC1||50.2|3|2|23|435/7|UBR|7|2|65|65|10|38|38|40665|3|Off Switch name|Up|VCC||||||||||||||| Switch name complete|Up|VCC| RTC2||50.1|12|1|411|16|VBR(NRTime)|3|2|65|65|10|128|128|128|1|On RTC1||50.2|3|2|23|106/9|UBR|7|2|65|65|10|38|38|40665|1|Off Switch name|Up|VCC||||||||||||||| Switch name complete|Up|VCC| RTC2||50.1|12|1|411|16|VBR(NRTime)|3|2|65|65|10|128|128|128|1|On RTC1||50.2|3|2|23|106/9|UBR|7|2|65|65|10|38|38|40665|1|Off


My problem is I need the Switch name on the single lines
but don't need them on the Switch name complete lines.

here is a copy of the script I use to build this file.
If anyone has any better ideas on how to build the file
or parse out the extra line on the file that was created
I would greatly appreciate it

I have spent two days attempting to parse out the first line
of the duplicate lines with nothing but frustration

#!/usr/bin/perl<br> <br> my %current = ();<br> my @fline = ();<br> my $original = "";<br> open (DATA, "/export/home/webadm/scripts/backup_scripts/backup_data/1s +tnodupe"); while (<DATA>) { my @line = split(/\|/); # split on | @current{qw/name var1 var2 var3 var4 var5 var6 var7 var8 var9 var1 +0 var11 var12 var13 var14 var15 var16 var17 var18/} = @line; #------------------------------ # process first two lines #----------- if ($line[0] =~ /^\S/) { # if line = Non-White Space Charac +ter (equal to first line in 3 line data) if (keys %current) { print; @fline = @line; $original = $line[0]; } } #--------------------------- # process overflow #--------- elsif ($line[0] =~ /^ (.*)/) { my $new = $line[0]; @next = split(/ /, $new); $overflow = $original.$next[1]; print "$overflow\|$fline[1]\|$fline[2]\|\n"; } #------------------------ # process rtc1 and rtc2 #----------- else { print; # print rest of lines } }


This file is pretty extensinve, almost 35,000 lines
with the only pattern being one partial switch name
followed by a full switch name
followed by rtc1 and rtc2

Thank you in advance,
Er1k the Rookie

Replies are listed 'Best First'.
Re: printing needed info only
by eejack (Hermit) on May 03, 2001 at 07:22 UTC
    I'm not sure this is what you are looking for but....
    #!/usr/bin/perl -w use strict; my $previous_line = ""; my $line = ""; open (DATA, "/export/home/webadm/scripts/backup_scripts/backup_data/1s +tnodupe"); while (<DATA>) { chop; $line = $_; if ($line =~ /^\S/) { # if line starts with Non-White Space Charac +ter if ($previous_line =~ /^\S/) { # if the previous line als +o # starts with Non-White Space +Character # then do something with your successful finding like... print "$previous_line\n"; } else { # do something if not } } $previous_line = $line; }
    I am guessing you want to find the first line of the two lines that don't start with a space. If you wanted to find the second line you could just print line instead of previous_line.

    If this is way off, try to explain it better and I'll give it another go.

    Thanks,

    EEjack

Re: printing needed info only
by cLive ;-) (Prior) on May 03, 2001 at 13:12 UTC
    Not exactly sure what you want. To print only the blocks beginning 'Switch name complete'?. If so, let's parse the data and see what we can do...
    my ($output,$match_flag); open (DATA, "/export/home/webadm/scripts/backup_scripts/backup_data/1s +tnodupe"); while (<DATA>) { if (/^Switch name complete/) { # match this $output .= $_; $match_flag=1; } elsif (/^\s/ && $match_flag) { # if correct, match this as well $output .= $_; } elsif (/^\S/) { # invalid, so don't match, or match following lines # beginning with whitespace $match_flag=0; } } close(DATA); print $output;

    That strips out only the sections beginning with 'Switch name complete'. (Note, I didn't test, but I think syntax is OK).

    cLive ;-)

      Thank you Guys for your assistnace

      I figured our a pattern and used it

      Line pattern as follows:
      /^\S/ # at least one followed by /^\s/ # and there are two lines of this


      The line I needed we just before the /^\s/ line.
      I was able to follow this pattern and print the desired
      info out to yet another file

      the following script is what I used:
      #!/usr/bin/perl -w use strict; my $previous_line = ""; my $line = ""; open (DATA, "/export/home/webadm/scripts/data/fourth_parse"); while (<DATA>) { chop; $line = $_; if ($line =~ /^\s/ && $previous_line =~ /^\S/) { print "$previous_line\n"; } else { if ($line =~ /^\sR*/ && $previous_line =~ /^\sR*/) { print "$previous_line\n"; print "$line\n"; } } $previous_line = $line; }
      Thank you again for your support
      Erik
Re: printing needed info only
by Er1kW (Initiate) on May 03, 2001 at 07:08 UTC
    This info has been manipulated to get it where it is now.

    I have been working on this with a co-worker and
    he is in the same situation I am
    Frustrated!

    Just need the last piece.

    Any suggestions would be appreciated.

    Thank you again,
    Er1kW