Login or Register to Reply

Thread Tools Search this Thread
# 1  

I have searched the forums for a solution but I haven't found a perfect answer, and I'm a bit of a novice, so I hope someone can help:

I have 2 files:


Chr1 139311 1/1:37,3,0:19
Chr1 139350 1/1:67,6,0:19
Chr1 139404 1/1:0,0,0:7
Chr1 152655 0/1:0,0,0:3
Chr1 152718 1/1:62,6,0:21
Chr1 277393 1/1:0,0,0:5
Chr1 277419 1/1:50,6,0:20
Chr1 277446 1/1:56,6,0:21
Chr1 280267 1/1:0,0,0:10
Chr1 280306 1/1:19,3,0:7
Chr1 280334 1/1:18,3,0:11


Chr1 139311 1
Chr1 139350 2
Chr1 139404 2
Chr1 152655 1
Chr1 152718 2
Chr1 277393 2
Chr1 277419 2
Chr1 277446 2
Chr1 280306 1
Chr1 280334 2

file1 contains all of the column1,column2 combinations found in file2, but not vice versa (i.e. some are present in file1 but not file2). I need to make a file with all 3 columns from file2, plus a new column with the appropriate column3 values from file1 to match column1 and column2 in the new file. The perfect output would be something like this:

Chr1 139311 1 1/1:37,3,0:19
Chr1 139350 2 1/1:67,6,0:19
Chr1 139404 2 1/1:0,0,0:7
Chr1 152655 1 0/1:0,0,0:3
Chr1 152718 2 1/1:62,6,0:21
Chr1 277393 2 1/1:0,0,0:5
Chr1 277419 2 1/1:50,6,0:20
Chr1 277446 2 1/1:56,6,0:21
Chr1 280306 1 1/1:19,3,0:7
Chr1 280334 2 1/1:18,3,0:11

I've been tinkering with awk but can't manage to find a good solution. Thanks very much in advance!

Last edited by Scott; 08-14-2012 at 03:44 PM.. Reason: Please use code tags, and a descriptive subject title for your threads
# 2  
awk 'NR==FNR{a[$1" "$2]=$3;next}{$4=a[$1" "$2]}1' file1 file2

This User Gave Thanks to bartus11 For This Post:
Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Need help on merging
I have a total of 100 files (variable size of each file) with total size of 328950 bytes. I want to merge those 100 files into 4 files with each size be close to equal size i.e (328950/4 ~= 82238) but do not want to break any file. Any unix sheel script help will be really helpful.... Shell Programming and Scripting
Shell Programming and Scripting
Merging two files with merging line by line
Hi, I have two files and i want to merge it like, file1.txt --------- abc cde efg file2.txt ------- 111 222 333 Output file should be, -------------- abc 111... Shell Programming and Scripting
Shell Programming and Scripting
Lucky Ali
Hi all, I have 2 files. I want to merge a portion or column in file 2 into file 1. file 1 - not tab or space delimited B_1 gihgjfhdj| hgfkddlldjljldjlddl B_2 gihgjddshjgfhs| hgfkddlldjljldjlddl B_3 gihgjfhdj| hgfkddlldjljldjlddlhgjdhdhjdhjhdjhdjhgdj file2 -...... Shell Programming and Scripting
Shell Programming and Scripting
merging files
vakharia Mahesh
Thanks in advance I have 2 files having key field in each.I would like to join both on common key.I have used join but not sucessful. The files are attached here . what i Want in the output is on the key field SLS OFFR . I have used join commd but not successful. File one ======= SNO ...... Shell Programming and Scripting
Shell Programming and Scripting
Merging Help
Hi Gurus, I need a help in merging the files. I have nearly 7 files and the files will have time stamp in it. I need to merger these files condition is it is not necessary that all the 7 files has to be there. suppose if i have only 3 files availabe out of these 7 then i need to merge...... Shell Programming and Scripting
Shell Programming and Scripting

Featured Tech Videos