#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $key1 = 'MSISDN'; my $key2 = 'CF'; my $sep = ','; # input my $rec = {}; while (my $line = ){ next if $line =~ /BEGINFILE/; #skip first line chomp $line; $line =~ s/^\s+|;$//g; # remove leading whitespace and ; if ($line =~ /SUBBEGIN/){ $rec = {}; # start new record } elsif ($line =~ /SUBEND/){ if (defined $rec->{$key1} && defined $rec->{$key2}){ output_record($rec) ; } } else { my ($key,$value) = split /=/,$line; push @{$rec->{$key}},$value if ($key); } } # output sub output_record { my $rec = shift; print Dumper \$rec; } __DATA__ #### ## sub output_record { my $rec = shift; # print Dumper \$rec; my $MSISDN = $rec->{$key1}[0]; # single my @CF = @{$rec->{$key2}}; # multiple for (@CF){ s{(CF.*-ALL-PROV)-NONE.*}{$1-1/1/1/0}; s{(CF.*-TS10-(?:REG|ACT))-91(\d*).*}{$1-1/1/1/0-$2}; } print join $sep,$MSISDN,@CF; print "\n"; }