?
Solved

Compare two files, find match, print match to third file

Posted on 2004-11-15
7
Medium Priority
?
800 Views
Last Modified: 2008-02-01
I've tried to work this out on my own, but my Perl skills are hurtin'..  I have two files which I want to compare and if a match is found, write output to a third file.  Thanks!  Rich

Input file 1 contains data in the following format:
856
E5B
4BB

Input file 2 contains data in the following format:
JOHNS-PA-WEFF01
SYRAC-NY-WE5B03
WILKE-PA-WF5202

I'd like to use data from file 1 to search for a match in file 2, and print the matching string from file 2 to an output file.  In this case, using the three samples above, output would be:
SYRAC-NY-WE5B03

Here's where I am:

open(RING, "<$file1") or die "Error opening $file1: $!";
open(MASTER, "<$file2") or die "Error opening $file2: $!";
open (HOSTS, ">$output") or die "Error opening $output: $!";
@file1 = <RING>;
foreach $ring(@file1) {
      while ($switch = (<MASTER>)) {
                chomp $switch;
            if ($ring =~ /$switch.*/) {
                  print HOSTS "$switch\n";
            }
      }
}
close(RING);
close(MASTER);
close(HOSTS);

0
Comment
Question by:rcris001
  • 4
  • 3
7 Comments
 

Author Comment

by:rcris001
ID: 12584510
Tried this variation..  Still nothing written to output file.  -Rich

open(RING, "<$file1") or die "Error opening $file1: $!";
open(MASTER, "<$file2") or die "Error opening $file2: $!";
open (HOSTS, ">$output") or die "Error opening $output: $!";
@file1 = <RING>;
foreach $ring(@file1) {
      while ($switch = (<MASTER>)) {
            if ($switch =~ /(([\D ]{5})-([\D]{2})-(W$ring([0-9]{2})))/i) {
                  print HOSTS "$switch\n";
            }
      }
}
close(RING);
close(MASTER);
close(HOSTS);
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 12584523
open(RING, "<$file1") or die "Error opening $file1: $!";
chomp(@file1 = <RING>);
close RING;
$re=join'|',map{quotemeta}@file1;
open(MASTER, "<$file2") or die "Error opening $file2: $!";
open (HOSTS, ">$output") or die "Error opening $output: $!";
while( <MASTER> ){
    print HOSTS if /$re/o;
}
close HOSTS;
close MASTER;

0
 
LVL 85

Expert Comment

by:ozo
ID: 12584591
Are the lines in file2 always of the form /(([\D]{5})-([\D]{2})-(W($ring)([0-9]{2})))/ ?

If so, we can speed up that loop a bit, and perhaps eliminate some false matches
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rcris001
ID: 12584634
Ozo, thanks man!  Works great.  Can you 'splain the line which reads:

$re=join'|',map{quotemeta}@file1;

 -Rich
0
 

Author Comment

by:rcris001
ID: 12584650
A to you Q:  Are the lines in file2 always of the form /(([\D]{5})-([\D]{2})-(W($ring)([0-9]{2})))/ ?

Yes, always in that form.
0
 
LVL 85

Expert Comment

by:ozo
ID: 12584667
open(RING, "<$file1") or die "Error opening $file1: $!";
chomp(@file1 = <RING>);
close RING;
@ring{@file1}=(1)x@file1;
open(MASTER, "<$file2") or die "Error opening $file2: $!";
open (HOSTS, ">$output") or die "Error opening $output: $!";
while( <MASTER> ){
    print HOSTS if /\D{5}-\D{2}-W(.+)\d{2}/ && $ring{$1};
}
close HOSTS;
close MASTER;
0
 
LVL 85

Expert Comment

by:ozo
ID: 12584745
$re=join'|',map{quotemeta}@file1;
quotemeta puts a \ in front of any character that might be special in a regular expression, like * + | ()
Just in case file1 contains data like
8+6
E|B
(B)
but it looks like it might always be alphanumeric, so perhaps that would not become an issue.
join'|' would produce   856|E5B|4BB
which in a regular expression would match any of the three samples
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question