How to find the matched numbers between 2 text file using perl program??

hi dudes, I nee you kind assistance, I have to find the matched numbers from 2 text files and output of matched numbers should be in another text file..
I do have text files like this , for example

File 1

787
665*5-p
5454
545-p
445-p
5454*-p

File 2

5455
787
445-p
4356
2445
144

Out put of matched numbers from 2 files is

787
445-p

I need perl program to solve this problem:confused:..Please guys help me.I need this program very urgent.. I am looking forward your program soooooon..

Thanks in Advance.
Suresh

Hi sureshraj,

Here a 'perl' script. With comments so you can at least try to solve similar problems for your own next time.

$ cat file-1
787
665*5-p
5454
545-p
445-p
5454*-p
$ cat file-2
5455
787
445-p
4356
2445
144                                                                                                                                                                                                                                          
$ cat script.pl
use warnings;                                                                                                                                                                                                                                
use strict;                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                             
## Check arguments.                                                                                                                                                                                                                          
die qq[Usage: perl $0 <file-1> <file-2>\n] unless @ARGV == 2;                                                                                                                                                                                
                                                                                                                                                                                                                                             
## Open both files.                                                                                                                                                                                                                          
open my $fh1, qq[<], shift @ARGV or die qq[Error: $!\n];                                                                                                                                                                                     
open my $fh2, qq[<], shift @ARGV or die qq[Error: $!\n];                                                                                                                                                                                     
                                                                                                                                                                                                                                             
## Hash to keep numbers of first processed file.                                                                                                                                                                                             
my %num;                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                             
## Read first file line by line.                                                                                                                                                                                                             
while ( <$fh1> ) {                                                                                                                                                                                                                           
                                                                                                                                                                                                                                             
        ## Remove leading and trailing spaces.                                                                                                                                                                                               
        s/^\s+|\s+$//g;                                                                                                                                                                                                                       
                                                                                                                                                                                                                                             
        ## Save number in a hash.                                                                                                                                                                                                            
        $num{ $_ } = 1;                                                                                                                                                                                                                      
}                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
## Read second file line by line.                                                                                                                                                                                                            
while ( <$fh2> ) {                                                                                                                                                                                                                           
                                                                                                                                                                                                                                             
        ## Remove leading and trailing spaces.                                                                                                                                                                                               
        s/^\s+|\s+$//g;                                                                                                                                                                                                                       
                                                                                                                                                                                                                                             
        ## If exists a hash entry means that same number was found in                                                                                                                                                                        
        ## first file, so print to output.                                                                                                                                                                                                   
        if ( exists $num{ $_ } ) {                                                                                                                                                                                                           
                printf qq[%s\n], $_;                                                                                                                                                                                                         
        }
}
$ perl script.pl file-1 file-2
787
445-p

Regards,
Birei

Dear Birei,

Thanks for your kind reply, But this script is not working, It is not giving out put file. I need a out text file as well. and how can i give my input, it is not reading exactly..Is this program for linux platform?? i am using windows.so could you please solve this one..I am new to perl program..please guide me

This is a 'Unix and Linux Forum', so I thought that your OS was between those. But 'perl' is a platform independent language, so it should work in Windows too.

Open a console and run the script, and tell us what do you see? A prompt? Normal output? Errors?

Regards,
Birei