Matching contents in two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching contents in two files
# 1  
Old 12-07-2012
Matching contents in two files

Hi,

I have two huge approximately 1 million rows files. I want to match contents from file 1 to 2 and append the file 2 matched contents to file 1

file1:
Code:
 chr7    Cufflinks       exon    12345678        23456789        .       -       .       gene_id "XLOC_02883"; transcript_id "TCONS_00033205"; exon_number "1"; oId "CUFF.20719.1"; tss_id "TSS25542";

file2:
Code:
chr7   unknown exon      12345678        23456789          .       +       .       gene_id "ABC_6"; transcript_id "NM_010920"; gene_name "ABC_6"; p_id "P5967"; tss_id "TSS2091";

If columns 3,4,5 of file 1 matches with column 3,4,5 of file 2 then assign the gene_id of file 2 to file 1.

output
Code:
 chr7    Cufflinks       exon    12345678        23456789        .       -       .       gene_id "XLOC_02883"; transcript_id "TCONS_00033205"; exon_number "1"; oId "CUFF.20719.1"; tss_id "TSS25542";gene_name "ABC_6"

Can I do this by simple shell scripting?

Thanks,
# 2  
Old 12-07-2012
Try:
Code:
awk 'NR==FNR{sub(";","",$10);a[$3" "$4" "$5]=$10;next}$3" "$4" "$5 in a{$0=$0"gene_name "a[$3" "$4" "$5]}1' file2 file1

# 3  
Old 12-09-2012
Code:
awk 'NR==FNR{split($0,b,";");a[$3 FS $4 FS $5]=b[3];next}{$0=$0 a[$3 FS $4 FS $5]}1' file2 file1

# 4  
Old 12-11-2012
Thank you..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

2. UNIX for Advanced & Expert Users

How to find duplicates contents in a files by comparing other files?

Hi Guys , we have one directory ...in that directory all files will be set on each day.. files must have header ,contents ,footer.. i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Discussion started by: Venkatesh1
7 Replies

3. Shell Programming and Scripting

I want to delete the contents of a file which are matching with contents of other file

Hi, I want to delete the contents of a file which are matching with contents of other file in shell scripting. Ex. file1 sheel,sumit,1,2,3,4,5,6,7,8 sumit,rana,2,3,4,5,6,7,8,9 grade,pass,2,3,4,5,6,232,1,1 name,sur,33,1,4,12,3,5,6,8 sheel,pass,2,3,4,5,6,232,1,1 File2... (3 Replies)
Discussion started by: ranasheel2000
3 Replies

4. Shell Programming and Scripting

Select the exact matching contents using grep

Hi everyone I've two files.. The contents of file1 are as shown below 4 5 12 13 36 37 45 46 47 The contents of file2 are as shown below 21 hello 13 world (5 Replies)
Discussion started by: abk07
5 Replies

5. Shell Programming and Scripting

Matching two file contents and extracting associated information

Hi, I am new to shell programming and need help. I have File1 with some ID numbers and File2 with ID number and some associated information. I want to match the ID numbers from File1 to contents in File2 and output a third file which pulls out the ID numbers and the associated information with... (2 Replies)
Discussion started by: newpro
2 Replies

6. Shell Programming and Scripting

common contents of two files

I have two files: file a with contents 1 2 3 4 5 file b with contents 6 3 5 8 9 10 i want go get file c which has the common contents of both files so file c should have contents 3 5 (9 Replies)
Discussion started by: tomjones
9 Replies

7. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

8. UNIX for Advanced & Expert Users

print contents of file2 for matching pattern in file1 - AWK

File1 row is same as column 2 in file 2. Also file 2 will either start with A, B or C. And 3rd column in file 2 is always F2. When column 2 of file 2 matches file1 column, print all those rows into a separate file. Here is an example. file 1: 100 103 104 108 file 2: ... (6 Replies)
Discussion started by: i.scientist
6 Replies

9. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies

10. Shell Programming and Scripting

Comparing contents of files

Hi, I hav two files a1.txt and a2.txt, a1.txt contains: --------------- asdev ebcdev .... a2.txt contains: --------------- asdev ebcdev prod .... a1.txt will be updated by a process,.. now i want to compare two files and i want to see data which is not in a1.txt am i clear....?? ... (3 Replies)
Discussion started by: rrs
3 Replies
Login or Register to Ask a Question