Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

parse a text file

Posted on 2007-11-24
2
Medium Priority
?
203 Views
Last Modified: 2010-03-05
Each alarm starts with UNIT6, I'm trying to get all the rows of each alarm into one row and from there I can sort and do whatever I need. I've had some success using while loops but the problem is some alarms have 4 rows,  some have 5,6, or 7. Once it gets beyond 4 rows I get duplicates or I only get alarms with 5 rows depending on how I set up the while loop. I attached the code I have been trying so far it is somewhat messy from all the retries.
Here are is a partial alarm log:
           UNIT6        LOT-0086  BLOCK-0086    ENVIR     2007-11-14  00:09:19.37
*   ALARM                         D142B      
   (10239) 7312 RATIO THRESHOLD                    
                N

           UNIT6        LOT-0086             ENVIR     2007-11-14  00:09:20.05
*   ALARM                                        
   (10241) 7000 CABINET OPEN                                                    
                Cabinet open.                                        
                02 01 00

           UNIT6        LOT-0086             COMM      2007-11-14  00:09:20.46
**  ALARM                                        
   (10242) 8999 BLOCKED FROM USE                                                
                PCBLOCKHub      PCBLOCKHub      CC bank            
                02 00 1d
#!/usr/bin/perl -w
use strict;
 
 
open DDDLOG,  "expx.txt" or die "Cannot open file $!";
open DDDRPT1, ">all.txt" or die "Cannot open file $!";
 
 
 
my @FFF; my $fff;
my @DDD; my $ddd;
my @NUM; my $num;
my @GGG; my $ggg;
my @DESC; my $desc;
my @DESC2; my $desc2;
my @DESC3; my $desc3;
 
 
########  PARSE RAW FILE ################
 
 
MAIN: while (<DDDLOG>){
            if (/\s+UNIT6\s+LOT-\d\d\d\d/){
            $fff = $_;
            @FFF = split /\s+/, $fff;
            #print "@FFF\n";
            #}
     
 
         LOOP2: while (<DDDLOG>){
                     redo MAIN if (/\s+UNIT6/);
                     if (/\*|\*\*|\*\*\*/){
                     $ddd = $_;
                     @DDD = split /\s+/, $ddd;
                     #print "@FFF @DDD\n";
                     #}
 
             LOOP3: while (<DDDLOG>){
                         redo MAIN if (/\s+UNIT6/);
                         if (/7\d\d\d|8\d\d\d/){
                         $num = $_;
                         @NUM = split /\s+/, $num;
                         #print DDDRPT1 "@FFF @DDD @NUM\n";
                         #}
 
                   LOOP4: while (<DDDLOG>){
                               redo MAIN if (/\s+UNIT6/);
                               if (/[a-zA-Z0-9]/){
                              $desc = $_;
                               #chomp($desc);
                               @DESC = split /\s+/, $desc;
                
                              #print DDDRPT1 "@FFF @DDD @NUM @DESC\n";
                               # print DDDRPT1 "$DESC[1] $DESC[2] $DESC[3]\n";
                 
                 
 
 
     LOOP5: while (<DDDLOG>){
                 redo MAIN if (/\s+UNIT6/);
                 if (/[a-zA-Z0-9]/){
                 $desc2 = $_;
                 #chomp($desc2);
                 @DESC2 = split /\s+/, $desc2;
                
               print DDDRPT1 "@FFF @DDD @NUM @DESC @DESC2\n";
                  # print DDDRPT1 "$DESC[1] $DESC[2] $DESC[3]\n";
                  #}  
                 #elsif(/ /){
                 #print DDDRPT1 "@FFF @DDD @NUM @DESC\n";
                 #last;
                 
            
     LOOP6: while (<DDDLOG>){
                 redo MAIN if (/\s+UNIT6/);
                 if (/[a-zA-Z0-9]/){
                 $desc3 = $_;
                 #chomp($desc3);
                 @DESC3 = split /\s+/, $desc3;
                
               #print DDDRPT1 "@FFF @DDD @NUM @DESC @DESC2 @DESC3\n";
                  # print DDDRPT1 "$DESC[1] $DESC[2] $DESC[3]\n";
                  }  
                 elsif(/ /){
                 #print DDDRPT1 "@FFF @DDD @NUM @DESC\n";
                 last;    
            
}}}}
   }
}
            }
            } 
            }
     }}
}
close DDDRPT1;
close DDDLOG;

Open in new window

0
Comment
Question by:omcr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 20343480
#!/usr/bin/perl -w
use strict;

open DDDLOG,  "expx.txt" or die "Cannot open file $!";
open DDDRPT1, ">all.txt" or die "Cannot open file $!";

my $e="";
while( <DDDLOG> ){
    chomp;
    print DDDRPT1 $e and $e="\n" if /^\s+UNIT6\s/;
    print DDDRPT1;
}
print DDDRPT1 $e;
close DDDRPT1;

0
 

Author Comment

by:omcr
ID: 20343665
Ozo,
Amazing. I'm not going say how much time (days) I've spent on this.
If you can take a second to explain it that would help. About the only thing I think I understand is $e adds a "\n" to the end everytime it sees UNIT6. Are you chomping every line in the file, I think so, and then newline in $e signals where to end. But I don't understand how you get each line into $e and make it one line without the  use of arrays?. Anyway there is one problem, the actual log file contains several lines of  trash at the beginning and end. I'm going to try to enclose your code in a while loop and see what happens
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question