#!/usr/bin/perl -w use strict; use File::Spec; my $spec_text = qr/Smt22/; my $path_tmp = $0; my ( $volume, $directories, $file ) = File::Spec->splitpath($path_tmp); my $path = $volume . $directories; opendir( DIRREN, $path ) || die; my @RENDIR = readdir(DIRREN); closedir( DIRREN ); my $match_file = "MSISDN.txt"; my @matches = (); my @errorcodes = qw/22 24 50 52 79/; # open file and load into array chdir $path; open( my $match_in, '<', $match_file ) or die("$match_file: open failed: $!"); my @matches_in = <$match_in>; close($match_in); for ( @matches_in ) { $_ = (split /,/)[1] . '.*Returned error code (?:' . join('|', @errorcodes) . ')'; } foreach my $file (@RENDIR) { if ( $file =~ $spec_text ) { open( my $fh_in, '<', $file ) or die("$file: open failed: $!"); while ( my $line = <$fh_in> ) { foreach my $match_in (@matches_in) { if ( $line =~ /$match_in/ ) { push @matches, $line; last; } } } close($fh_in); } } my @newmatch = (); my @tmplist = (); #split logfile line into needs and put together everything ;D foreach my $match (@matches) { chomp $match; @newmatch = split( ';', $match ); my @tmpnewmatch = split( ',', $newmatch[6] ); #cuts the blanks @ beginning and end of each string element $newmatch[5] =~ s/^\s+//; $tmpnewmatch[0] =~ s/^\s+//; push @tmplist, $newmatch[5] . ";" . $tmpnewmatch[0]; } # removes duplicates and print out MD Del list my @list = do { my %seen; grep !$seen{$_}++, @tmplist }; open( INFILE, '<', $match_file ) or die("$match_file: open failed: $!"); open( MDDELOUT, '>', "MD_delete_list.txt" ) or die("MD_delete_list.txt: open failed: $!"); while () { my @mdmsisdn = split( ',', $_ ); $mdmsisdn[1] =~ s/^\s+//; foreach my $mddel (@list) { my @newmddel = split( ';', $mddel ); if ( $newmddel[0] == $mdmsisdn[1] ) { print MDDELOUT "$mdmsisdn[0] ; $mddel \n"; } } } close(INFILE); close(MDDELOUT); #put the list into an array to easyly search for msisdn, will be put into a hash of hashes # top-level hash keyed by errorcode, lower-level hash keyed by msisdn value my %adv = (); my $errcode_regex = 'ADV: Returned error code (' . join('|',@errorcodes) . ')'; foreach my $list (@list) { my ( $msisdn, $adverror ) = split( ';', $list ); if ( $adverror =~ /$errcode_regex/ ) { my $errcode = $1; $adv{$errcode}{$msisdn} = $adverror; } else { print "no ADV Errors ;D" } } #open errorfiles and check if msisdn is already known then sorting to existing or not for my $errcode ( @errorcodes ) { my $advname = "adv$errcode.txt"; open( my $exmsisdn, '<', $advname ) or die("$advname: open failed: $!"); my @exerrors = <$exmsisdn>; close($exmsisdn); my @tmpnew = (); my @tmpold = (); foreach my $advkey ( keys %{$adv{$errcode}} ) { if ( grep { $advkey == $_ } @exerrors ) { print "$advkey is an old error\n"; push @tmpold, $advkey . "\n"; } else { print "$advkey is a new error\n"; push @tmpnew, $advkey . "\n"; } } #put new errors in arry to existing errors dupe check it and write back to file foreach my $tmpexout (@tmpnew) { push @exerrors, $tmpexout; } my @newexout = do { my %seen; grep !$seen{$_}++, @exerrors }; open( OUT, '>', $advname ) or die("$advname: open failed: $!"); foreach my $newout (@newexout) { print OUT $newout; } #printing out final records to mail ;D my $mailname = join '', 'adv', $errcode, '_mail.txt'; open( OUTMAIL, '>', $mailname ) or die("$mailname: open failed: $!"); print OUTMAIL "Here goes the text needed befor MSISDN \n NEW MSISDNs\n\n"; foreach my $mail (@tmpnew) { print OUTMAIL "$mail"; } print OUTMAIL "\n EXISTING MSISDNs\n"; foreach my $mailold (@tmpold) { print OUTMAIL "$mailold"; } close(OUTMAIL); }