I think I did not explain it well earlier. Here is a brief overview of what I am trying to do. I am merging 2 files based on certain criteria.

1. One rules states that if the line starts with a "2" copy the line as it is and write it to the output file.
2. If positions 14-16 have 800, 866, 877 or 888 then merge these items from the second file and arbitrarily change other positions to a predefined value.

As it stands now, before any modifications, this works, however, positions 14-16 that do not have 800, 866, 877 or 888 also need to be copied without modification to the output file. Of note here it is important that I write these in the order they are encountered in the original input file.

That being said after the modifications I have in effect an exact copy of my original input file and I am stumped.

Here is a copy of my working script before modifications:

#!/usr/bin/perl -w use File::Copy; my($input_file1) = $ARGV[0]; my($input_file2) = $ARGV[1]; my($output_file) = $ARGV[2]; if ( !defined($input_file1) || !defined($input_file2) || !defined($out +put_file) ) { print "Error: usage: mergefiles input_file1 input_file2 output_fil +e\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( $input_file2, $input_file2 . ".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( INFILE2, $input_file2 ) || die( "Could not read input file 2 + ($input_file2): $!" ); open( OUTPUT, "> " . $output_file ) || die( "Could not open output + file ($output_file): $!" ); # -----Read input_file2 into an array so that (later) we can do a +binary search----- @input2 = <INFILE2>; while (;INFILE1>) { my $line = $_; chomp($line); # -----A line starting with a '2' is a header and is left unch +anged 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 ); $term_id = substr( $line2, 39, 1); # -----From File2----- $carrier_info = substr( $line2, 44, 5 ); $destination_number = substr( $line2, 122, 10 ); $connect_time = substr( $line, 54, 6 ); $from_RAO = substr( $line, 71, 3 ); $indicator100 = substr( $line, 99, 1 ); $CABS_RAO = substr( $line, 109, 3 ); $settlement = substr( $line, 148, 1 ); $send_to_OCN = substr( $line, 186, 4 ); $record_type = "xx"; $from_RAO = "xxx"; $indicator100 = "x"; $CABS_RAO = "xxx"; $settlement = "x"; $send_to_OCN = "xxxx"; # -----Generate the output string----- $output_line = substr( $line, 0, 4 ) . $record_typ +e . $date . substr( $line, 12, 17 ) . $number_dialed . $term_id . substr( $line, 4 +0, 4 ) . $carrier_info . substr( $line, 49, 5 ) . $connect_time . substr( $line, 60, 11 ) . $from_RAO . substr( $line, 74, 25 ) . $indicator100 . substr( $line, 100, 9 ) . $CABS_RAO . substr( $line, 112, 10 ) . $destination_number . substr( $line, 132, 16 + ) . $settlement . substr( $line, 149, 37 ) . $send_to_OCN . substr( $line, 190, 20 ) . "\ +n"; print OUTPUT $output_line; # -----If we have found the line, we want to exit +the loop----- last; } } } else { print OUTPUT $line . "\n"; } } # -----Close all of the files----- close( INFILE1 ); close( INFILE2 ); close( OUTPUT ); }


Comments, observations and help are very much welcome. Special thanks to ikegame for your insight.

In reply to Re^6: Ignoring lines in flat text file by sheasbys
in thread Ignoring lines in flat text file by sheasbys

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.