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

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);

rcris001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rcris001Author Commented:
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
ozoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

rcris001Author Commented:
Ozo, thanks man!  Works great.  Can you 'splain the line which reads:

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

 -Rich
0
rcris001Author Commented:
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
ozoCommented:
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
ozoCommented:
$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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.