use File::Copy; use Tie::File; my($input_file1) = $ARGV[0]; my($input2) = $ARGV[1]; my($output_file) = $ARGV[2]; if ( !defined($input_file1) || !defined($input2) || !defined($output_file) ) { print "Error: usage: ./SS7Merge input_file1 input_file2 output_file\n"; } else { # -----Backup the input files in case of error----- copy( $input_file1, $input_file1 . ".bak" ) or die "Could not backup file 1 $input_file1 to $input_file1.bak: $!\n"; copy( $input2, $input2 . ".bak" ) or die "Could not backup file 2 $input_file2 to $input_file2.bak: $!\n"; # -----Attempt to open all of the files----- open( INFILE1, $input_file1 ) || die( "Could not read input file 1 ($input_file1): $!" ); open( OUTPUT, "> " . $output_file ) || die( "Could not open output file ($output_file): $!" ); # We are going to read file2 into an array. The file will not be loaded into memory which will improve processing of large files. tie @input2, 'Tie::File', \*FH, or die "Problem tying file $input2: $!"; while () { my $line = $_; chomp($line); # -----A line starting with a '2' is a header and is left unchanged if ( $line !~ m/^2/ ) { foreach $line2 (@input2) { $date = substr( $line, 6, 6 ); $number_dialed = substr( $line, 29, 10 ); if ( index( $line2, $date ) != -1 and index( $line2, $number_dialed ) != -1 ) { $record_type = substr( $line, 5, 2 ); # -----From File2----- $carrier_info = substr( $line2, 44, 5 ); $destination_number = substr( $line2, 122, 10 ); $connect_time = substr( $line, 54, 6 ); $send_to_OCN = substr( $line, 186, 4 ); $record_type = "25"; $send_to_OCN = "2604"; -----Generate the output string----- $output_line = substr( $line, 0, 4 ) . $record_type . $date . substr( $line, 12, 17 ) . $number_dialed . substr( $line, 39, 5 ) . $carrier_info . substr( $line, 49, 5 ) . $connect_time . substr( $line, 60, 62 ) . $destination_number . substr( $line, 132, 54 ) . $send_to_OCN . substr( $line, 190, 20 ) . "\n"; # -----Debug code. Add in if you are experiencing problems----- # print OUTPUT $output_line; # print STDOUT "Output " . ++$outputcount . "\n"; last; } } } else { print OUTPUT $line . "\n"; } } # Untie the array before closing the file use untie @input2; # -----Close all of the files----- close( INFILE1 ); close( OUTPUT ); }