How to find the matching data b/w 2 files in perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find the matching data b/w 2 files in perl?
# 1  
Old 05-06-2010
How to find the matching data b/w 2 files in perl?

Hi friends,,

i have find the matching data between 2files.
My file1 have a data like
Code:
rs3001336
rs3984736
rs2840532

File2 have a data like
Code:
rs3736330	  1	 2359237	A	G	0.28	   	1.099	        0.010
rs2840532	  1	 2359977	G	A	0.363 	0.3373	1.123	
rs3001336	  1	 2365193	G	A	0.0812	0.07319	1.12	
rs1129333	  1	 2367838	A	G	0.0817	0.0731	1.126	
rs3984736	  1	 2371236	G	A	0.0975	0.08456	1.133	
rs4531246	  1	 2394182	A	C	0.315	        0.9488	0.1

I have to find the matching data from file1 into the file2..
The output format should be like
Code:
rs3001336	  1	 2365193	G	A	0.0812	0.07319	1.12
rs2840532	  1	 2359977	G	A	0.363 	0.3373	1.123	
rs3984736	  1	 2371236	G	A	0.0975	0.08456	1.133

i need a program (in perl or R) to do this task.. pls friends help me

Thanks
Suresh

Last edited by pludi; 05-06-2010 at 03:19 AM.. Reason: code tags, please...
# 2  
Old 05-06-2010
Code:
for i in `cat file1 | sort | uniq`;do grep -w $i file2; done



---------- Post updated at 12:06 PM ---------- Previous update was at 11:42 AM ----------

how it is not working. can you please post your message here.. instead of private message.. so that some one help you....
Code:
kamaraj@kamaraj-laptop:~/Desktop/testing$ for i in `cat file1 | sort | uniq`;do grep -w $i file2; done
rs2840532	  1	 2359977	G	A	0.363 	0.3373	1.123	
rs3001336	  1	 2365193	G	A	0.0812	0.07319	1.12	
rs3984736	  1	 2371236	G	A	0.0975	0.08456	1.133	
kamaraj@kamaraj-laptop:~/Desktop/testing$ cat file1
rs3001336
rs3984736
rs2840532
kamaraj@kamaraj-laptop:~/Desktop/testing$ cat file2
rs3736330	  1	 2359237	A	G	0.28	   	1.099	        0.010
rs2840532	  1	 2359977	G	A	0.363 	0.3373	1.123	
rs3001336	  1	 2365193	G	A	0.0812	0.07319	1.12	
rs1129333	  1	 2367838	A	G	0.0817	0.0731	1.126	
rs3984736	  1	 2371236	G	A	0.0975	0.08456	1.133	
rs4531246	  1	 2394182	A	C	0.315	        0.9488	0.1
kamaraj@kamaraj-laptop:~/Desktop/testing$

# 3  
Old 05-06-2010
One way to do it with Perl -

Code:
$
$
$ cat file1
rs3001336
rs3984736
rs2840532
$
$ cat file2
rs3736330         1      2359237        A       G       0.28            1.099           0.010
rs2840532         1      2359977        G       A       0.363   0.3373  1.123
rs3001336         1      2365193        G       A       0.0812  0.07319 1.12
rs1129333         1      2367838        A       G       0.0817  0.0731  1.126
rs3984736         1      2371236        G       A       0.0975  0.08456 1.133
rs4531246         1      2394182        A       C       0.315           0.9488  0.1
$
$ perl -lane '$ARGV eq "file1" ? $x{$_}=1 : defined $x{$F[0]} ? print : ""' file1 file2
rs2840532         1      2359977        G       A       0.363   0.3373  1.123
rs3001336         1      2365193        G       A       0.0812  0.07319 1.12
rs3984736         1      2371236        G       A       0.0975  0.08456 1.133
$
$

tyler_durden
# 4  
Old 05-06-2010
"Easy to read Perl"
Code:
#!/usr/bin/perl

use strict;
use warnings;

my $file1='file1' ;
my $file2='file2' ;
my @temparr=();
my @mainarr=();

open(FIL1, $file1) or die "Error: $!\n" ;

   my @arr1 = <FIL1> ;

close(FIL1) ;

open(FIL2, $file2) or die "Error: $!\n";

   my @arr2 = <FIL2> ;

close(FIL2) ;

  foreach my $record (@arr1) {
  chomp $record ;
  @temparr = grep(/$record/,@arr2) ;
  push(@mainarr, @temparr) ;
  }

print @mainarr ;

Code:
$ perl checkmatch.pl | sort
rs2840532	  1	 2359977	G	A	0.363 	0.3373	1.123	
rs3001336	  1	 2365193	G	A	0.0812	0.07319	1.12	
rs3984736	  1	 2371236	G	A	0.0975	0.08456	1.133	
$

# 5  
Old 05-06-2010
Hi,

Looks like GWAS data
do you use PLINK? it has an option to do this

or you can do it using awk

Code:
awk 'FNR==NR {a[$1]++;next} $1 in a' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

Field matching in two data files

Hello, I am looking to output all of the lines from file2 whose 11th field is present in the first field in file1. Then the second field from file1 should be appended as such: file1: 2222 0.35 4444 0.25 5555 0.75 file2: col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 1111 col1 col2... (4 Replies)
Discussion started by: palex
4 Replies

3. Shell Programming and Scripting

Find matching timestamps in two files.

OK. if the first file is : 3184 2014-07-28 04:15 global.Remote-Access 10.111.8.25 81.245.6.25 tcp 3268 3035 2014-07-28 04:16 global.Remote-Access 10.111.8.12 81.245.6.25 tcp 3268If the second file is: 1 Jul 28 04:12 2014-07-28 id967254(BGC-TLW-Cert) Tunneling: User with IP... (8 Replies)
Discussion started by: fxsme
8 Replies

4. UNIX for Dummies Questions & Answers

find Search - Find files not matching a pattern

Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction. My question is as follows, I need to find files and possible folders which are not owner = AAA group = BBB with a said location and all sub folders ... (7 Replies)
Discussion started by: kilobyter
7 Replies

5. Shell Programming and Scripting

glob not matching all files in perl

I encountered a weird issue with globbing in perl not returning all files, while I was testing out a script for recursive dir-processing on my Synology NAS. Basically it didn't show (/match?) all the files in a specific directory. If I move the file to a different directory or even rename it, it... (2 Replies)
Discussion started by: odyssey
2 Replies

6. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

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 must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

7. Shell Programming and Scripting

Help to retrieve data from two files matching a string

Hello Experts, I have come back to this forum after a while now, since require a better way to get my result.. My query is as below.. I have 3 files -- 1 Input file, 2 Data files .. Based on the input file, data has to be retreived matching from two files which has one common key.. For EX:... (4 Replies)
Discussion started by: shaliniyadav
4 Replies

8. Shell Programming and Scripting

Compare two files and get matching data

Hi, Can anyone help me to compare two files and get the matching data... say i have file1 and file2 ... file1 has 300 unique data with that i need to match with file2 to see how may are matching.. file2 have 1000 records. (4 Replies)
Discussion started by: zooby
4 Replies

9. Programming

How to find the matching data b/w 2 files in perl?

Hi friends,, i have find the matching data between 2files. My file1 have a data like rs3001336 rs3984736 rs2840532 File2 have a data like rs3736330 1 2359237 A G 0.28 1.099 0.010 rs2840532 1 2359977 G A 0.363 0.3373 1.123 rs3001336 1 2365193 G A 0.0812 0.07319 1.12 ... (1 Reply)
Discussion started by: sureshraj
1 Replies

10. Shell Programming and Scripting

Identify matching data in a file and output to original line, in perl

Hi, I haven't done this for awhile, and further, I've never done it in perl so I appreciate any help you can give me. I have a file of lines, each with 5 data points that look like this: AB,N,ALLIANCEBERNSTEIN HLDNG L.P,AB,N ALD,N,ALLIED CAPITAL CORPORATION,ALD,N AFC,N,ALLIED CAPITAL... (4 Replies)
Discussion started by: Pcushing
4 Replies
Login or Register to Ask a Question