[bash] join command

Hi

I've 2 files:

I'd like to get an output like:

if I do like:

join file1 file2

I get

Where line 30 miss. I'm reading this old post:
Join command.
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:

the join command work correctly.

Try with awk...

 
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,$2,$3}}' file1 file2

many thanks it works! :slight_smile:

D.

I see you have an awk answer..

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

from the man page:

   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:

# 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

Just of curosity, why was the join command not working, malcomex?? any idea?

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.