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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find the matched numbers between 2 text file using perl program??
# 1  
Old 01-20-2012
Bug 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
Code:
787
665*5-p
5454
545-p
445-p
5454*-p

File 2
Code:
5455
787
445-p
4356
2445
144

Out put of matched numbers from 2 files is
Code:
787
445-p

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

Thanks in Advance.
Suresh

Last edited by Franklin52; 01-20-2012 at 10:46 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-20-2012
Hi sureshraj,

Here a 'perl' script. With comments so you can at least try to solve similar problems for your own next time.
Code:
$ 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
# 3  
Old 01-20-2012
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
# 4  
Old 01-20-2012
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a matched pattern and perform comparison on numbers next to it

Hi, I have been trying to extract rows that match pattern "cov" with the value next to it to be > 3. The 'cov' pattern may appear either in $3 or $4 (if using ";" as field separator). Below is the example:- input file ... (7 Replies)
Discussion started by: bunny_merah19
7 Replies

2. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

3. Shell Programming and Scripting

How to find numbers in text file?

Hi I have a text file with rows like this: 7 Herman ASI-40 Jungle (L) Blueprint (L) Weapon Herman ASI-40 Jungle (L) 215.00 57 65.21 114.41 and 9 Herman CAP-505 (L) Blueprint (L) Weapon Herman CAP-505 (L) 220.00 46.84 49.1 104.82 and 2 ClericDagger 1C blueprint Melee - Shortblade... (2 Replies)
Discussion started by: pesa
2 Replies

4. Homework & Coursework Questions

[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools. This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input. Sample input from <> operator: Hello World This is hello a sample... (2 Replies)
Discussion started by: D2K
2 Replies

5. Homework & Coursework Questions

program to find and print a Fibonacci sequence of numbers. --Errors

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to convert a C language program over to Sparc Assembley and I am getting Undefined first referenced... (4 Replies)
Discussion started by: kenjiro310
4 Replies

6. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

7. Shell Programming and Scripting

Perl program for find one entry and put in different file

Hi I have a file name1 xxxxx name1 xxxxx name1 yyyyy name 1 zzzzz name1 Uniprot Id 1234 name2 sssss name2 eeeee name2 bengamine name2 Uniprot Id 3456 ......................and so on I have to capture Uniprot IDs only in a separate file so that output contain only ... (20 Replies)
Discussion started by: manigrover
20 Replies

8. Shell Programming and Scripting

How to get the lines matched of a file in perl?

Hi, I want to match the time in the file and retrieve those contents of the file. I am taking only first two parameters of localtime(time) function minutes and seconds so partial match i am performing. For Example $start = "14:23"; $end = "14:30"; I am matching file contents... (3 Replies)
Discussion started by: vanitham
3 Replies

9. Shell Programming and Scripting

HELP! PERL script to find matched pattern

Hi all, I just learnt Perl and I encountered a problem in my current project. For a verilog file, i am required to write a PERL script that could match pattern to output nitrolink and nitropack. I wont know what name to grep except the pattern below. the verilog file: nitrolink nitrolink... (1 Reply)
Discussion started by: kimhuat
1 Replies

10. Post Here to Contact Site Administrators and Moderators

program to find the uncommon numbers between two files.

Hi, I need to extract the uncommon numbers from file1 and file2 For Example: File1 1 2 3 4 5 File2 1 2 3 4 5 6 (2 Replies)
Discussion started by: salaathi
2 Replies
Login or Register to Ask a Question