Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

[bash] join command

Shell Programming and Scripting


Tags
file comparison, join

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-17-2009   -   Original Discussion by Dedalus
Dedalus's Unix or Linux Image
Dedalus Dedalus is offline
Registered User
 
Join Date: May 2009
Last Activity: 25 April 2013, 11:48 AM EDT
Posts: 102
Thanks: 9
Thanked 1 Time in 1 Post
[bash] join command

Hi

I've 2 files:
Quote:
file 1:
4 3.855143e-07 3.855143e-07
6 1.927572e-07 5.782715e-07
7 3.855143e-07 9.637858e-07
8 1.927572e-07 1.156543e-06
30 4.018987e-05 8.086162e-05

file 2:
1 2.918198e-03 2.918198e-03
2 2.205868e-02 2.497688e-02
3 2.396582e-03 2.737346e-02
4 9.962099e-07 2.737445e-02
7 9.962099e-08 2.737455e-02
30 1.195452e-06 2.737575e-02
31 4.114347e-05 2.741689e-02
I'd like to get an output like:
Quote:
file output:

4 3.855143e-07 3.855143e-07 9.962099e-07 2.737445e-02
7 3.855143e-07 9.637858e-07 9.962099e-08 2.737455e-02
30 4.018987e-05 8.086162e-05 1.195452e-06 2.737575e-02
if I do like:



Code:
join file1 file2

I get
Quote:
4 3.855143e-07 3.855143e-07 9.962099e-07 2.737445e-02
7 3.855143e-07 9.637858e-07 9.962099e-08 2.737455e-02
Where line 30 miss. I'm reading this old post:
https://www.unix.com/unix-dummies-que...n-command.html.
Where a solutionn with awk is suggested.
I mean is possible get the same result with join command?

Thanks

D.

P.S. if file1 looks like:
Quote:
4 3.855143e-07 3.855143e-07
6 1.927572e-07 5.782715e-07
7 3.855143e-07 9.637858e-07
30 4.018987e-05 8.086162e-05
the join command work correctly.
Sponsored Links
    #2  
Old Unix and Linux 08-17-2009   -   Original Discussion by Dedalus
malcomex999's Unix or Linux Image
malcomex999 malcomex999 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 10 November 2015, 2:16 AM EST
Location: In the beautiful World...
Posts: 511
Thanks: 10
Thanked 29 Times in 29 Posts
Try with awk...



Code:
 
awk 'NR==FNR{arr[$1]?arr[$1]=arr[$1]FS$2FS$3:arr[$1]=$2FS$3}NR!=FNR{ for(i in arr){if(i == $1)print i,arr[i],$2,$3}}' file1 file2

Sponsored Links
    #3  
Old Unix and Linux 08-17-2009   -   Original Discussion by Dedalus
Dedalus's Unix or Linux Image
Dedalus Dedalus is offline
Registered User
 
Join Date: May 2009
Last Activity: 25 April 2013, 11:48 AM EDT
Posts: 102
Thanks: 9
Thanked 1 Time in 1 Post
many thanks it works! Linux

D.
    #4  
Old Unix and Linux 08-17-2009   -   Original Discussion by Dedalus
Tytalus's Unix or Linux Image
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
 
Join Date: Jun 2003
Last Activity: 19 May 2015, 6:39 AM EDT
Location: Scotland
Posts: 594
Thanks: 1
Thanked 28 Times in 27 Posts
I see you have an awk answer..

wrt the join - if you sort your files before running join it will work - little gotcha :-)

from the man page:



Code:
   file1 and file2  must  be  sorted  in  increasing  collating
     sequence  as determined by LC_COLLATE on the fields on which
     they are to be joined, normally the first in each line  (see
     sort(1)).

And here with your input:



Code:
# sort file1
30 4.018987e-05 8.086162e-05
4 3.855143e-07 3.855143e-07
6 1.927572e-07 5.782715e-07
7 3.855143e-07 9.637858e-07
8 1.927572e-07 1.156543e-06


HTH
Sponsored Links
    #5  
Old Unix and Linux 08-17-2009   -   Original Discussion by Dedalus
mac4rfree's Unix or Linux Image
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 9 September 2015, 1:36 AM EDT
Posts: 283
Thanks: 8
Thanked 0 Times in 0 Posts
Just of curosity, why was the join command not working, malcomex?? any idea?
Sponsored Links
    #6  
Old Unix and Linux 08-17-2009   -   Original Discussion by Dedalus
Dedalus's Unix or Linux Image
Dedalus Dedalus is offline
Registered User
 
Join Date: May 2009
Last Activity: 25 April 2013, 11:48 AM EDT
Posts: 102
Thanks: 9
Thanked 1 Time in 1 Post
hi

about the join command.
I read that Lesser-known Linux commands: join, paste, and sort
the problem is that the 2 files doesn't have a common field. So a solution could be to use the nl tool. (as it suggest).

but i don't understand why if i remove the line 8, it works. there are still line 1, 2 and 6 that don't match.

Thx
D.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Join command Mish_99 Shell Programming and Scripting 1 10-24-2008 11:27 AM
Help with using join command yahyaaa Shell Programming and Scripting 4 06-15-2008 01:16 PM
join (pls help on join command) summer_cherry Shell Programming and Scripting 1 12-31-2007 04:19 AM
Using Join Command Supial Shell Programming and Scripting 4 08-09-2005 01:12 AM
Join command unisam UNIX for Dummies Questions & Answers 4 07-28-2004 06:59 AM



All times are GMT -4. The time now is 12:22 PM.