Hi,
   I have two big text files, I need to read one line from first file, write
some information from this line to a new file, and search second file to
find lines with same control_id, and write more information to the new file,
I wrote in perl, but it tooks half day to finish joining the two files.
   Do you have any suggest?

   Below are some of my code.
====================================================================

#!/usr/bin/perl -w
#use IO::FILE;
#use strict 'subs';
#

$file1="file1.txt";
$file2="file2.txt";

open (SOURCE, "$file1")
        or die "can't open the $file1: $!";

while (<SOURCE>) {
        $control_id = substr($_, 0, 22);

        open (SINK, ">>newFile.dat")
                or die "can't open the newFile.dat: $!";

        print SINK $control_id;
        #write more to newFile.dat

        open (ADDSOURCE, "$file2")
                or die "can't open the $file2: $!";

        while (<ADDSOURCE>) {
                if ($_ =~ /^$control_id/) {
                        print SINK substr($_, 31, 3);
                        #write more to newFile.dat
                        $weight = substr($_, 48, 7);
                        $totalWeight += $weight;
                        $_ = <ADDSOURCE>;
                        while ($_ =~ /^$control_id/) {
                                print SINK substr($_, 31, 3);
                                #write more to newFile.dat
                                $weight = substr($_, 48, 7);
                                $totalWeight += $weight;
                                $_ = <ADDSOURCE>;
                                }#end of while
                        print SINK "$totalWeight";
                        seek(ADDSOURCE, 0, 2)
                                or die "Couldn't seek to the end: $!\n";

                        }#end of if
                }#end of while for ADDSOURCE

        close(ADDSOURCE) or die "can't close $ADDSOURCE: $!\n";
        close(SINK) or die "can't close $SINK: $!\n";
        } #end of while for SOURCE
  close(SOURCE) or die "can't close $SOURCE: $!\n";


============================================================================
===========

Reply via email to