Merging two text files by a column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Merging two text files by a column
# 1  
Old 08-18-2011
Merging two text files by a column

So I have two text files. The first one looks like this:

refsnp_id chr_name chrom_start
1 rs1000000 12 126890980
2 rs10000010 4 21618674
3 rs10000012 4 1357325
4 rs10000013 4 37225069
5 rs1000002 3 183635768

And the second one looks like this:

AUC rs1000000 0.03 0.1240
AUC rs10000010 0.03 0.1462
AUC rs10000012 0.00 0.8628
AUC rs10000013 0.00 0.5459
AUC rs1000002 0.00 0.6439
AUC rs10000023 0.03 0.1337
AUC rs10000027 0.00 0.7142
AUC rs10000030 0.00 0.7634
AUC rs1000003 0.02 0.2226


The second columns of the two text files match but not completely. There are some missing lines in the first text file. I want to merge the two text files by their second columns and add the third and fourth column of the first file to the second file when the second columns of the two files are matching. How do I go about doing that? Thanks!
# 2  
Old 08-18-2011
Post desired output for this data.
# 3  
Old 08-18-2011
AUC rs1000000 0.03 0.1240 12 126890980
AUC rs10000010 0.03 0.1462 4 21618674
AUC rs10000012 0.00 0.8628 4 1357325
AUC rs10000013 0.00 0.5459 4 37225069
AUC rs1000002 0.00 0.6439 3 183635768

Thanks!
# 4  
Old 08-18-2011
Try:
Code:
join -12 -22 -o 2.1,2.2,2.3,1.3,1.4 <(sort -k2,2 file1) <(sort -k2,2 file2)

# 5  
Old 08-19-2011
Quote:
Originally Posted by bartus11
Try:
Code:
join -12 -22 -o 2.1,2.2,2.3,1.3,1.4 <(sort -k2,2 file1) <(sort -k2,2 file2)

You can even use "join -j 2" instead of "join -1 2 -2 2" Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging 2 text files when there is a common time stamp column in them

Dear Unix experts and users I have 2 kinds of files like below, of which I need to merge them in the order of time. File1: Date_Time Context D1 D2 04/19/2013_23:48:54.819 ABCD x x 04/19/2013_23:48:55.307 ABCD x x 04/19/2013_23:48:55.823 ABCD x ... (7 Replies)
Discussion started by: ks_reddy
7 Replies

2. Shell Programming and Scripting

Merging columns based on one or more column in two files

I have two files. FileA.txt 30910 rs7468327 36587 rs10814410 91857 rs9408752 105797 rs1133715 146659 rs2262038 152695 rs2810979 181843 rs3008128 182129 rs3008131 192118 rs3008170 FileB.txt 30910 1.9415219673 0 36431 1.3351312477 0.0107191428 36587 1.3169171182... (2 Replies)
Discussion started by: genehunter
2 Replies

3. UNIX for Dummies Questions & Answers

Merging two text files by a column and filling in the missing values

Hi, I have to text files that I want to merge by the first column. The values in the first column pretty much match for the first part. However there are some values that are present in column 1 and not present in column 2 or vice versa. For such values I would like to substitute X for the... (9 Replies)
Discussion started by: evelibertine
9 Replies

4. UNIX for Dummies Questions & Answers

Merging two text files by a column

I have two text files. One has two columns and looks like below: rs# otherallele_freq rs10399749 0 rs4030303 0 rs4030300 0 rs940550 1.000 rs13328714 0 rs11490937 0 rs6683466 0 rs12025928 1.000 rs6650104 0 rs11240781 0... (5 Replies)
Discussion started by: evelibertine
5 Replies

5. Shell Programming and Scripting

merging two files based on first column

I had two files file1 and file2. I want a o/p file(file3) like below using first column as ref. Pls give suggestion ass join is not working as the number of lines in each file is nealry 5 C? file1 --------------------- 404000324810001 Y 404000324810004 N 404000324810008 Y 404000324810009 N... (1 Reply)
Discussion started by: p_sai_ias
1 Replies

6. Shell Programming and Scripting

Merging 2 files based on a common column

Hi All, I do have 2 files file 1 has 4 tab delimited columns 234 a c dfgyu 294 b g fih 302 c h jzh 328 z c san 597 f g son File 2 has 2 tab delimted columns 234 23 302 24 597 24 I want to merge file 2 with file 1 based on the data common in both files which is the first column so... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

7. Shell Programming and Scripting

merging column from two files based on identifier

Hi, I have two files consisting of two columns. So I want to merge column 2 if column 1 is the same. So heres an example of what I mean. FILE1 driver 444 car 333 hat 222 FILE2 driver 333 car 666 hat 999 So I want to merge the column 2's together so... (4 Replies)
Discussion started by: phil_heath
4 Replies

8. Shell Programming and Scripting

Merging column files

Hi,Iam new to Unix.I have a file FileA which is a variable length file where each column is seperated by delimitter "|". FileA: SrNo Name Address 1-234|name1|Addr1 1-34|name2|Addr2 1-2345|name3|Addr3 FileB: SrNo Address 1-34<<06 SPACES>>Addr1<<8 spaces>> 1-234<<05... (1 Reply)
Discussion started by: swapna321
1 Replies

9. Shell Programming and Scripting

Merging two files with a common column

Hi, I have two files file1 and file2. I have to merge the columns of those two files into file3 based on common column of two files. To be simple. file1: Row-id name1 13456 Rahul 16789 Vishal 18901 Karan file2 : Row-id place 18901 Mumbai ... (2 Replies)
Discussion started by: manneni prakash
2 Replies

10. Shell Programming and Scripting

merging text files

hi, i need to merge 2 lakh text files ..... can somebody please help me with a script/program for it... (8 Replies)
Discussion started by: code19
8 Replies
Login or Register to Ask a Question