Home Man
Search
Today's Posts
Register

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

Tags
file comparison, join, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 08-17-2009
[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.
# 2  
Old 08-17-2009
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

# 3  
Old 08-17-2009
many thanks it works!

D.
# 4  
Old 08-17-2009
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
# 5  
Old 08-17-2009
Just of curosity, why was the join command not working, malcomex?? any idea?
# 6  
Old 08-17-2009
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.
Login to Reply

« Previous Thread | Next 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
Join columns across multiple lines in a Text based on common column using BASH nv186000 Shell Programming and Scripting 6 03-06-2018 08:14 AM
BASH join command error PLS deadcick UNIX for Beginners Questions & Answers 1 02-10-2017 07:38 PM
How to join 2 text files using bash scripting? H.R Shell Programming and Scripting 3 04-09-2016 01:00 AM
Bash: join 2 files MumuGB Shell Programming and Scripting 4 02-03-2014 10:17 AM
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
how to join two files using "Join" command with one common field in this problem? mindfreak UNIX for Dummies Questions & Answers 2 04-13-2012 05:55 AM
Bash join script not working Nostyx Shell Programming and Scripting 2 11-29-2011 03:56 PM
awk command for simple join command but based on 2 columns ruby_sgp Shell Programming and Scripting 2 03-28-2010 07:00 AM
join (pls help on join command) summer_cherry Shell Programming and Scripting 1 12-31-2007 04:19 AM
join command penfold Shell Programming and Scripting 22 02-22-2005 07:37 AM


All times are GMT -4. The time now is 08:23 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password