0

I am trying to join two files with different rows but one element in common.

FILE1:

1/temperature
2/rainfall
3/snowfall
4/windspeed
5/winddirection

FILE2:

2008-10-01/1/68
2008-10-02/2/6
2008-10-03/3/4
2008-10-04/4/25
2008-10-05/5/120
2008-10-01/1/89
2008-10-01/4/35
2008-11-01/5/360
2008-10-01/1/45
2008-12-01/1/61
2008-10-10/1/32

I am trying to join both files using the following command

join -t/ -1 1 -2 2 FILE1 FILE2

I expect the following output:

1/temperature/2008-10-01/68
1/temperature/2008-10-01/89
1/temperature/2008-10-01/45
1/temperature/2008-12-01/61
1/temperature/2008-10-10/32
2/rainfall/2008-10-02/6
3/snowfall/2008-10-03/4
4/windspeed/2008-10-04/25
4/windspeed/2008-10-01/35
5/winddirection/2008-10-05/120
5/winddirection/2008-11-01/360

but I am having the first 5 rows in this way:

1/temperature/2008-10-01/68
2/rainfall/2008-10-02/6
3/snowfall/2008-10-03/4
4/windspeed/2008-10-04/25
5/winddirection/2008-10-05/120

Please your help what I am doing wrong

1

join expects its inputs to be sorted (on the fields that are to be joined). In this case (making use of the bash shell's process substitution):

$ join -t/ -1 1 -2 2 FILE1 <(sort -t/ -k2 FILE2)
1/temperature/2008-10-10/32
1/temperature/2008-10-01/45
1/temperature/2008-12-01/61
1/temperature/2008-10-01/68
1/temperature/2008-10-01/89
2/rainfall/2008-10-02/6
3/snowfall/2008-10-03/4
4/windspeed/2008-10-04/25
4/windspeed/2008-10-01/35
5/winddirection/2008-10-05/120
5/winddirection/2008-11-01/360

Note that the required sorting is lexical rather than numeric - if the first column values in FILE1 exceed 9, then you will likely need to pre-sort that as well.

| improve this answer | |
  • Many thanks my friend. finally worked. Steeldriver you deserve a medal – Gonzalo Ponce Jun 24 '19 at 2:37
  • @GonzaloPonce Good! If this solves your issue, please consider "accepting" the answer. This is the best way to show gratitude on this site. Accepting an answer not only marks the question as resolved, but also signals to future readers that the accepted answer actually solved the issue. More information about this is available here: unix.stackexchange.com/help/someone-answers – Kusalananda Jun 24 '19 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.