Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update a txt file wioth another txt file.

Status
Not open for further replies.

rkckjk

IS-IT--Management
Apr 27, 2001
34
US
I have the following tab delimited master.txt file which has about 1000 records and looks like this:

NTADPH0853M00 Premier WST None "Lee, Tom" 4607 "Lee, Tom"
NTADPH0892M00 Standard; Prime Shift Monitoring WST None "JOHNSON, DON"
ntadph0919m00 Standard; Prime Shift Monitoring wst None "Lee, Tom"
ntadph0961m00 Standard; Prime Shift Monitoring WST None "Wayne, John"
NTADPH0964L01 Premier WST None "Smith, Adam" 3786 "Smith, Adam"

Then I have a tab delimited update.txt file which has about 1000 records also and looks like this:

NTAPTH0236M01 19687 307 1 1
NTADPH0853M00 31612 508 9 24
NTAPTH0271M00 21735 307 4 4
ntadph0919m00 19072 301 2 3
NTADPH0964L01 18000 307 4 4

I want to create a Perl script that will check for matches with the update.txt file against the master.txt file. If a match is found the master.txt file would be appended with the last four fields from the update.txt file.
There are 3 records from the update.txt file that are found in the master.txt file in the above example.

The result would look like this:

NTADPH0853M00 Premier WST None "Lee, Tom" 4607 "Lee, Tom" 31612 508 9 24
ntadph0919m00 Standard; Prime Shift Monitoring wst None "Lee, Tom" 19072 301 2 3
NTADPH0964L01 Premier WST None "Smith, Adam" 3786 "Smith, Adam" 18000 307 4 4

I also want to produce an error listing of all the records in the update.txt file that don't match up with the master.txt file.

thanks


 
Code:
#!/usr/bin/perl

@master = ('NTADPH0853M00 Premier WST None "Lee, Tom" 4607 "Lee, Tom"',
           'NTADPH0892M00 Standard; Prime Shift Monitoring WST None "JOHNSON, DON"',
           'ntadph0919m00 Standard; Prime Shift Monitoring wst None "Lee, Tom"',
           'ntadph0961m00 Standard; Prime Shift Monitoring WST None "Wayne, John"',
           'NTADPH0964L01 Premier WST None "Smith, Adam" 3786 "Smith, Adam"');
           
@update = ('NTAPTH0236M01 19687 307 1 1',
           'NTADPH0853M00 31612 508 9 24',
           'NTAPTH0271M00 21735 307 4 4',
           'ntadph0919m00 19072 301 2 3',
           'NTADPH0964L01 18000 307 4 4');

foreach $master (@master) {
  $master =~ m|^(.{13})|;
  $master_code = $1;
  foreach $update (@update) {
    $update =~ m|^(.{13})(.*)$|;
    if ($master_code eq $1) {
      print "$master$2\n";
    }
  }
}


Kind Regards
Duncan
 
Try this:-
Read both files into vars.
Compare the first 13 chrs of each update file with the first 13 chrs of the master file (nested FOR loops).
Append any matches by splitting the update file(substr) and adding the result to the end of the master file.
Hope this make sense
Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top