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

Greetings Perl Masters, I hope this is an easy solution. I have a stateful log where I have to match up transaction ID's and based on information in the first part of the log I perform an operation on the second part of the log. Here is sample of the log:
============================================== CALL_INIT 2008/02/25 23:40:02.126 TRAN_ID 0000afab CGI 420-1-303-10689 CHAN N/A CHAN_MODE Speech Version 1 CHAN_TYPE TCH/F SUB_CHAN 0 TIMESLOT 3 TSC 7 HOPPING Enabled MAIO 0 HSN 20 MA 777,808,820 ENCR A5/1 SESSION_KEY A35CEEBE61150AD8 RESPONSE_TIME 12500 ms ============================================== CALL_RESPONSE 2008/02/25 23:40:02.888 TRAN_ID 0000afac CGI 420-1-303-10689 PLR_RESPONSE_TIME 13500 ms LCS CAUSE Unexpected data value in position request ============================================== CALL_RESPONSE 2008/02/25 23:40:14.636 TRAN_ID 0000afab CGI 420-1-302-13448 PLR_RESPONSE_TIME 13500 ms Px,Py = bfbc76df8932b7b9,3faca201ba5e7998 UNCERTAINTY MAJOR 0x30 (960 m) UNCERTAINTY MINOR 0x30 (960 m) CONFIDENCE 67 % TYPE OF SHAPE 3 LOCATION METHOD 8 ============================================== CALL_RESPONSE 2008/02/25 23:42:27.627 TRAN_ID 0000afb7 CGI 420-1-301-15401 PLR_RESPONSE_TIME 13500 ms LCS CAUSE Unexpected data value in position request ============================================== CALL_INIT 2008/02/25 23:42:28.746 TRAN_ID 0000afb8 CGI 420-1-315-12057 CHAN N/A CHAN_MODE Signaling Only CHAN_TYPE SDCCH/8 SUB_CHAN 1 TIMESLOT 2 TSC 0 HOPPING Enabled MAIO 0 HSN 1 MA 799,822,842,844,848 ENCR A5/1 SESSION_KEY A78C8953CE40CC00 RESPONSE_TIME 12500 ms ============================================== CALL_RESPONSE 2008/02/25 23:42:29.777 TRAN_ID 0000afb9 CGI 420-1-302-13448 PLR_RESPONSE_TIME 13500 ms LCS CAUSE Unexpected data value in position request
If the CALL_INIT contains TCH then I run the decode program on the CALL_RESPONSE on the PX,PY line. Currently the decode program is a simple c script that prints the information right to the screen. But unfortunately, my boss wants me to replace the PX,PY with the information from the C program. Thanks to the amazing help on this forum I have a script that does everything I need but prints the decoded information at the bottom of the output text. The code logic so far discards all the information in the rest of the log, so there is no easy way I can think of replacing the PX,PY with the data. Here is the code.
#!/usr/bin/perl use constant MAX_OPEN_CALLS => 1000; use strict; use warnings; my %call; { local $/ = '=============================================='; while (<>) { my ($type, $id, $info) = parse_rec($_); print "$_\n"; if ($type eq 'INIT') { next if $info !~ /TCH/; $call{$id} = 1; } elsif ($type eq 'RESPONSE') { next if ! exists $call{$id}; (my @args) = $info =~ /(.{8})(.{8}),(.{8})(.{8})/; my $cprogram = `./decrypt @args`; print $cprogram; print "\n"; delete $call{$id}; } die "Too many unanswered calls" if keys %call > MAX_OPEN_CALLS +; } } sub parse_rec { my ($rec) = @_; my ($type) = $rec =~ /CALL_(\w+)/; $type = '' if ! defined $type; my ($id) = $rec =~ /TRAN_ID\s+(\S+)/; my $info; if ($type eq 'INIT') { ($info) = $rec =~ /CHAN_TYPE\s+(\S+)/; } elsif ($type eq 'RESPONSE') { ($info) = $rec =~ /Px,Py\s*=\s*(\S+)/; } $info = '' if ! defined $info; return ($type, $id, $info); }
This is a sample of what the script currently does:
============================================== CALL_RESPONSE 2008/02/25 23:40:14.636 TRAN_ID 0000afab CGI 420-1-302-13448 PLR_RESPONSE_TIME 13500 ms Px,Py = bfbc76df8932b7b9,3faca201ba5e7998 UNCERTAINTY MAJOR 0x30 (960 m) UNCERTAINTY MINOR 0x30 (960 m) CONFIDENCE 67 % TYPE OF SHAPE 3 LOCATION METHOD 8 ============================================== LAT 39.0712913394 LONG -77.4686356683
This is what is should do:
============================================== CALL_RESPONSE 2008/02/25 23:40:14.636 TRAN_ID 0000afab CGI 420-1-302-13448 PLR_RESPONSE_TIME 13500 ms LAT 39.0712913394 LONG -77.4686356683 UNCERTAINTY MAJOR 0x30 (960 m) UNCERTAINTY MINOR 0x30 (960 m) CONFIDENCE 67 % TYPE OF SHAPE 3 LOCATION METHOD 8 ==============================================

Replies are listed 'Best First'.
Re: Simple redesign to Log Parser!
by Illuminatus (Curate) on Sep 19, 2008 at 20:46 UTC
    Hi Josh, tell Dave he can format his own report :)

    Your issue is that you print right after you call parse_rec. You will probably want to move the print to just above the die, and within the elsif, modify $_ (use either a regex or split/join).

    Contact me at drjohnson1984@hotmail.com if you need further help. Drew