cuboidgraphix Posted November 11, 2008 Share Posted November 11, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/132338-perl-how-can-i-parse-the-content-in-this-text-file-correctly/ Share on other sites More sharing options...
effigy Posted November 11, 2008 Share Posted November 11, 2008 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' } ]; Quote Link to comment https://forums.phpfreaks.com/topic/132338-perl-how-can-i-parse-the-content-in-this-text-file-correctly/#findComment-688045 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.