Jump to content

[PERL] How can I parse the content in this text file, correctly?


Recommended Posts

Hello,

Perl is new to me and I'm trying to get my script right.

 

For starters.. below is the text file content that I want to parse:

 

open cper 
Done. 
SP******  ** CPER100 NOV11 14:03:00 2100 INFO CDMA Performance Snapshot  
        BSC: 0   Projected CSS ENGCAP:  56000 
        CAU :  36:5  59:5 
        ------------------------------------- 
        Origination Attempts:            1538 
        Origination Successes:           1318 
        %Success:                         85% 
        ------------------------------------- 
        Page Responses:                   560 
        Termination Successes:            546 
        %Success:                         97% 
        ------------------------------------- 
        Hard Handoff Attempts:             10 
        Hard Handoff Successes:            10 
        %Success:                        100% 
        ------------------------------------- 
        Estimated CCR (wrt CSS ENGCAP):   88%

 

 

Sometimes the text file is captured with the line "open cper " and sometimes it is not. What I need to do is to have my perl script find the word "Done." and start counting lines from there.

 

Below is my code so far.

 


#!/usr/bin/perl 

use Mysql; 
open (MYFILE, $ARGV[0]) or die "oops: $!"; 

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); 
$DATE=sprintf("%4d-%02d-%02d\n",$year+1900,$mon+1,$mday); 
chop($DATE); 

print "DATE:", $DATE, "\n"; 

############## 
## Get Data ## 
############## 

while(my $line = <MYFILE>){ 
if($line =~ m/^Done./){ 
        $count++; 
        } 
if($count == 1){ 
        $TIME = substr $line, 27, 8; 
        print "TIME:", $TIME; 
} elsif($count == 4){ 
        $ORIGATT = substr $line, 41, 5; 
        print "ORIGATT:", $ORIGATT; 
        } elsif($count == 5){ 
        $ORIGSUCC = substr $line, 41, 5; 
        print "ORIGSUCC:", $ORIGSUCC; 
        } elsif($count == { 
        $PAGERESP = substr $line, 41, 5; 
        print "PAGERESP:", $PAGERESP; 
        } elsif($count == 9){ 
        $TERMSUCC = substr $line, 41, 5; 
        print "TERMSUCC:", $TERMSUCC; 
        } elsif($count == 12){ 
        $HHOFFATT = substr $line, 41, 5; 
        print "HHOFFATT:", $HHOFFATT; 
        } elsif($count == 13){ 
        $HHOFFSUCC = substr $line, 41, 5; 
        print "HHOFFSUCC:", $HHOFFSUCC; 
        } elsif($count == 16){ 
        $ESTIMCCR = substr $line, 41, 3; 
        print "ESTIMCCR:", $ESTIMCCR, "\n"; 
        $count++; 
        } else{ 
        } 
} 

close(MYFILE);

What is your desired data structure? You could just pull what you need via regex:

 

use strict;
use warnings;
use Data::Dumper;

my %tmp;
my @data;

while (<DATA>) {
if (/^\s*-+\s*$/ && %tmp) {
	push @data, { %tmp };
	%tmp = ();
}
next if ! /^\s*([^:]+):\s+(\d+)%?\s*$/;
$tmp{$1} = $2;
}
push @data, { %tmp };
print Data::Dumper->Dump([\@data]);

__DATA__
open cper
Done.
SP******  ** CPER100 NOV11 14:03:00 2100 INFO CDMA Performance Snapshot 
        BSC: 0   Projected CSS ENGCAP:  56000
        CAU :  36:5  59:5
        -------------------------------------
        Origination Attempts:            1538
        Origination Successes:           1318
        %Success:                         85%
        -------------------------------------
        Page Responses:                   560
        Termination Successes:            546
        %Success:                         97%
        -------------------------------------
        Hard Handoff Attempts:             10
        Hard Handoff Successes:            10
        %Success:                        100%
        -------------------------------------
        Estimated CCR (wrt CSS ENGCAP):   88%

$VAR1 = [

          {

            '%Success' => '85',

            'Origination Attempts' => '1538',

            'Origination Successes' => '1318'

          },

          {

            'Termination Successes' => '546',

            '%Success' => '97',

            'Page Responses' => '560'

          },

          {

            '%Success' => '100',

            'Hard Handoff Successes' => '10',

            'Hard Handoff Attempts' => '10'

          },

          {

            'Estimated CCR (wrt CSS ENGCAP)' => '88'

          }

        ];

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.