Help with using join command

Dear,

Im using Join command but I have the below problem.

first file1 was

BSS01 101 BSC_Olymbia_SDH 02/06/2008 22:59:08
BSS01 107 Swafieh 14/06/2008 11:11:39
BSS01 147 Kalha_Rep 11/06/2008 19:13:39
BSS01 147 Kalha_Rep 11/06/2008 19:13:39
BSS05 504 BSC37_Cement_Factory 05/06/2008 08:10:27
BSS06 601 Abu_Alanda_Caragat 12/06/2008 02:57:11
BSS06 637 Souq_Al_Khudra 14/06/2008 23:20:16
BSS07 749 Petra_Mobilcar_NEW 12/06/2008 11:37:46
BSS07 749 Petra_Mobilcar_NEW 12/06/2008 11:37:46
BSS13 1301 Hamlan_2 19/03/2008 11:59:17
BSS14 1435 Kufer_Khall 14/06/2008 09:47:39
BSS17 1717 Firas_MW_Node 14/06/2008 14:18:21
BSS18 1804 Drugs_Resistance 08/06/2008 20:37:25
BSS38 3806 Action_Target_MobileCar_NEW 06/06/2008 00:51:47
BSS39 3907 Taj_Mall_Mobilcar 14/06/2008 21:06:28
BSS39 3908 Dier_Ghbar_2_Mobilecar 14/06/2008 22:25:02
BSS39 3908 Dier_Ghbar_2_Mobilecar 14/06/2008 22:25:02
BSS39 3908 Dier_Ghbar_2_Mobilecar 14/06/2008 22:25:02

file2

101 34
107 45
147 345
504 343
601 23423
637 23423
749 34234
1301 4556
1424 454
1435 464
1717 76768
1804 4566

I used the second coloum in the first file and the first coloum in the second file as the key fields and I want to
print the first 3 fields of the first file and the second field of file2

I used join -2j 1 -j1 2 -o 1.1 1.2 1.3 2.2 file1 file2 > Output and I sorted them as you see.
the Output was like this

BSS01 101 BSC_Olymbia_SDH 34
BSS01 107 Swafieh 45
BSS01 147 Kalha_Rep 345
BSS01 147 Kalha_Rep 345
BSS05 504 BSC37_Cement_Factory 343
BSS06 601 Abu_Alanda_Caragat 23423
BSS06 637 Souq_Al_Khudra 23423
BSS07 749 Petra_Mobilcar_NEW 34234
BSS07 749 Petra_Mobilcar_NEW 34234

is there another way to make it work .... :frowning:
any help is appreciated.
Best Regards

What output do you want - join worked as you asked. give us an example.

if you notice, the output is not complete it's missing some results,
as an example.

1804 is avaliable in both files but I cant find it in the output.

You can try something like this with awk:

awk '
NR==FNR{a[$1]=$2;next}
a[$2]{print $1, $2, $3, a[$2];next}1' file2 file1

Regards

Mann. your quick response, help and kindness make me always speechless.
Thanks so much,

You are the best :wink: