Matching two files with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching two files with awk
# 1  
Old 02-06-2013
Matching two files with awk

Hello,

I have two files as follow:
Code:
AFFY_ID RS_ID CHROMOSOME POS_START POS_END ALLELE1 ALLELE2
SNP_A-1780283  rs17011450  chr4 127630275 127630276 C T
SNP_A-1780285  rs6919430  chr6 90919464 90919465 A C
SNP_A-1780286  ---  chr7 104281409 104281410 A G
SNP_A-1780301  rs2342723  chr16 5748790 5748791 C T

And the second file:
Code:
SNP_A-1780301 2 181.55 G A  
SNP_A-1780305 2 140.448 C T  
SNP_A-1780321 2 142.446 T C  
SNP_A-1780346 2 6.62269 T C  
SNP_A-1780368 2 137.474 A G

What I need is to match the first columns of two files and print the second column of the first file into the second file, so, the output would be:

SNP_A-1780301 rs2342723 2 181.55 G A

How could I do this with AWK?

Thanks a lot!

---------- Post updated at 08:50 AM ---------- Previous update was at 08:45 AM ----------

I have written this code but it doesn't work as I wish:

Code:
 awk 'NR==FNR{A[$1]=$0;next}{if(A[$1]){print$0, $2, A[$1]}}' file2 file1 >output


Last edited by Franklin52; 02-06-2013 at 09:54 AM.. Reason: fixed code tags
# 2  
Old 02-06-2013
Try:
Code:
awk 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1] FS $2}1' file1 file2 > newfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk for matching fields between files with repeated records

Hello all, I am having trouble with what should be an easy task, but seem to be missing something fundamental. I have two files, with File 1 consisting of a single field of many thousands of records. I also have File 2 with two fields and many thousands of records. My goal is that when $1 of... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

Awk: matching multiple fields between 2 files

Hi, I have 2 tab-delimited input files as follows. file1.tab: green A apple red B apple file2.tab: apple - A;Z Objective: Return $1 of file1 if, . $1 of file2 matches $3 of file1 and, . any single element (separated by ";") in $3 of file2 is present in $2 of file1 In order to... (3 Replies)
Discussion started by: beca123456
3 Replies

3. Shell Programming and Scripting

awk to print matching lines in files that meet critera

In the tab delimited files below I am trying to match $2 in file1 to $2 of file2. If a match is found the awk checks $3 of file2 and if it is greater than 40% and $4 of file2 is greater than 49, the line in file1 is printed. In the desired output line3 of file1 is not printed because $3 off file2... (9 Replies)
Discussion started by: cmccabe
9 Replies

4. Shell Programming and Scripting

awk two succeeding lines and moving matching files

Hello everyone I have a few hundreds of .mol2 files that has this pattern @<TRIPOS>ATOM 2 H18 65.2220 Du 1 RES1 0.0000 @<TRIPOS>BOND 1 3 5 ar @<TRIPOS>SUBSTRUCTURE among them, some of the files are missing the line after the @<TRIPOS>BOND and they look... (2 Replies)
Discussion started by: Error404
2 Replies

5. Shell Programming and Scripting

Help with awk Matching columns from two files

Hello, I have two files as following: #bin chrom chromStart chromEnd name score strand observed 585 chr2 29442 29443 rs4637157 0 + C/T 585 chr2 33011 33012 rs13423995 0 + A/G 585 chr2 34502 34503 rs13386087 0 + ... (2 Replies)
Discussion started by: Homa
2 Replies

6. Shell Programming and Scripting

AWK help, matching 2 files into one

I'm newbie with AWK. What I'm trying to do is matching file1 and file2 into a file3 with records listed in columns with pipe as delimiter. The thing is the file1 has thousands of records while file2 has very few. But I want the file3 to show all records in file1 and with data from file2 to be... (2 Replies)
Discussion started by: jmeasel7
2 Replies

7. Shell Programming and Scripting

AWK - Comparing/Matching/Counting with 2 files

I have 2 files that I want to do some comparing on. First, I want to find the unique list of devices in file1 and then put them to a new file, file2. I was able to do this without any problem with the following statement: cat file1 | awk '{print $2}' | awk '!x++' > file2Here is what I can't... (2 Replies)
Discussion started by: jontjioe
2 Replies

8. Shell Programming and Scripting

awk - Matching columns between 2 files and reordering results

I am trying to match 4 colums (first_name,last_name,dob,ssn) between 2 files and when there is an exact match I need to write out these matches to a new file with a combination of fields from file1 and file2. I've managed to come up with a way to match these 2 files based on the columns (see below)... (7 Replies)
Discussion started by: ambroze
7 Replies

9. Shell Programming and Scripting

AWK: matching patterns in 2 different files

In a directory, there are two different file extensions (*.txt and *.xyz) having similar names of numerical strings (*). The (*.txt) contains 5000 multiple files and the (*.xyz) also contains 5000 multiple files. Each of the files has around 4000 rows and 8 columns, with several unique string... (5 Replies)
Discussion started by: asanjuan
5 Replies

10. Shell Programming and Scripting

AWK Matching Fields and Combining Files

Hello! I am writing a program to run through two large lists of data (~300,000 rows), find where rows in one file match another, and combine them based on matching fields. Due to the large file sizes, I'm guessing AWK will be the most efficient way to do this. Overall, the input and output I'm... (5 Replies)
Discussion started by: Michelangelo
5 Replies
Login or Register to Ask a Question