Join files by second column | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Join files by second column

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-19-2013
radius radius is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 18 September 2014, 12:31 AM EDT
Posts: 86
Thanks: 35
Thanked 0 Times in 0 Posts
Join files by second column

I have file input

file1


Code:
1/1/2013    A    553.0763397    96    16582
1/1/2013    B    459.8333588    195    11992
1/2/2013    A    844.2973022    306    19555
1/2/2013    B    833.9300537    457    20165
1/3/2013    A    563.6917419    396    13879
1/3/2013    B    632.0749969    169    21205
1/4/2013    C    48.33055687    41    1596

file2


Code:
A    X1    X3    X5    X7    X9
B    X2    X3    X6    X7    X9
C    X1    X4    X5    X8    X9
D    X3    X2    X1    X4    X8

I need to join file1 and file2 ..i expect the output tobe:

file output


Code:
1/1/2013    A    553.0763397    96    16582    X1    X3    X5    X7    X9
1/1/2013    B    459.8333588    195    11992    X2    X3    X6    X7    X9
1/2/2013    A    844.2973022    306    19555    X1    X3    X5    X7    X9
1/2/2013    B    833.9300537    457    20165    X2    X3    X6    X7    X9
1/3/2013    A    563.6917419    396    13879    X1    X3    X5    X7    X9
1/3/2013    B    632.0749969    169    21205    X2    X3    X6    X7    X9
1/4/2013    C    48.33055687    41    1596    X1    X4    X5    X8    X9

thanks for the help

Last edited by radius; 07-19-2013 at 03:22 AM..
Sponsored Links
    #2  
Old 07-19-2013
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 9 April 2014, 1:31 AM EDT
Location: INDIA, Bangalore
Posts: 2,049
Thanks: 16
Thanked 106 Times in 102 Posts
try...


Code:
awk 'NR==FNR{v=$1;$1="";A[v]=$0;next}
{print $0" "A[$2]}' file2 file1

The Following User Says Thank You to vidyadhar85 For This Useful Post:
radius (07-19-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to use the the join command to join multiple files by a common column evelibertine UNIX for Dummies Questions & Answers 5 07-05-2012 04:15 PM
join two files based on one column newpro Shell Programming and Scripting 3 04-26-2012 05:07 PM
Join 3 or more files using matching column cyz700 Shell Programming and Scripting 3 04-27-2011 08:04 AM
Join multiple files by column with awk macsx82 Shell Programming and Scripting 10 09-18-2010 04:56 PM
Join 2 files using first column vanesa1230 UNIX for Dummies Questions & Answers 2 06-26-2010 11:45 PM



All times are GMT -4. The time now is 07:46 PM.