use strict; use warnings; sub printRecord; #-------------------------------------------------- # Parsing loop #-------------------------------------------------- my $fhOut = \*STDOUT; my $iLevel=0; my %hFields; while (my $sLine = ) { #if line defines the level, set level if ($sLine =~ /^\s*(?:Level|Record|Sub Record)\s+\(\d+\)/) { $iLevel++; } elsif ($sLine =~ /^\s*End of/) { $iLevel--; } else { my ($k, $v) = $sLine =~ /\s+\"(\w+)\"\s+=\s+\"([^"]*)\"/; $hFields{$k}=$v; } #if level back to 0, dump record if ($iLevel == 0) { printRecord($fhOut, \%hFields); %hFields=(); } } #-------------------------------------------------- # SUBROUTINE DEFINITIONS #-------------------------------------------------- sub printRecord { my ($fhOut, $hFields) = @_; my $sIOType = $hFields->{MSC_CDR_TYPE}; print $fhOut "RECORD\n"; print $fhOut "#addkey\n"; print $fhOut "#filename FF\n"; print $fhOut "#input_id 001\n"; print $fhOut "#input_type $sIOType\n"; print $fhOut "#output_id\n"; print $fhOut "#output_type $sIOType\n"; print $fhOut "#source_id SRC\n"; foreach my $k (sort keys %$hFields) { my $v = $hFields->{$k}; print $fhOut "F $k $v\n"; } print $fhOut ".\n"; } #cut and paste sample data from above __DATA__