Help with using join command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with using join command
# 1  
Old 06-15-2008
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 .... Smilie
any help is appreciated.
Best Regards
# 2  
Old 06-15-2008
What output do you want - join worked as you asked. give us an example.
# 3  
Old 06-15-2008
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.
# 4  
Old 06-15-2008
You can try something like this with awk:

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

Regards
# 5  
Old 06-15-2008
Mann. your quick response, help and kindness make me always speechless.
Thanks so much,

You are the best Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Join Command

Hi, Please explain the working process of join command. File 1 P B S A C AFile2 C B P A S DBut the output of join command is... join File1.txt File2.txt P B A S A DBut I guess the output should be P B A S A D C A BPlease correct me,if i am worong or missing some thing. Thanks (2 Replies)
Discussion started by: satyar
2 Replies

2. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

3. UNIX for Dummies Questions & Answers

how to join two files using "Join" command with one common field in this problem?

file1: Toronto:12439755:1076359:July 1, 1867:6 Quebec City:7560592:1542056:July 1, 1867:5 Halifax:938134:55284:July 1, 1867:4 Fredericton:751400:72908:July 1, 1867:3 Winnipeg:1170300:647797:July 15, 1870:7 Victoria:4168123:944735:July 20, 1871:10 Charlottetown:137900:5660:July 1, 1873:2... (2 Replies)
Discussion started by: mindfreak
2 Replies

4. Shell Programming and Scripting

awk command for simple join command but based on 2 columns

input1 a_a a/a 10 100 a1 a_a 20 200 b1 b_b 30 300 input2 a_a a/a xxx yyy a1 a1 lll ppp b1 b_b kkk ooo output a_a a/a 10 100 xxx yyy (2 Replies)
Discussion started by: ruby_sgp
2 Replies

5. Shell Programming and Scripting

need help with join command

Hello, I am using join to merge two files together. The defaults usually works great join file1 file2 However, sometimes file1 or file2 has more keys, which I want to keep. file 1: -1 z 0 a 1 b 2 c file 2: 0 a2 1 b2 2 c2 3 c3 So I do join -a1 -a2 file1 file2 But then you don't... (0 Replies)
Discussion started by: ayu
0 Replies

6. Shell Programming and Scripting

join command

All, I have 3 files (tab seperated): Note: Please treat dash (-) as empty value in the file, I have added it to make input easily readable file1: (2 cols) A 1 B 2 C 3 file2: (4 cols - col4 has empty values for 4th column except 2nd row) A 1 5 - B 2 6 Y C 3 7 - D 4 8 - file3... (1 Reply)
Discussion started by: bgmmk
1 Replies

7. Shell Programming and Scripting

Join command

Dear Experts, I have several (say 'm') text files, each with 'n' columns. I want to put them into a large single file with n*m columns. a 1 a 1 a 1 a 1 b 2 b 5 b 1 b 3 c 3 c 7 ... (1 Reply)
Discussion started by: Mish_99
1 Replies

8. Shell Programming and Scripting

join (pls help on join command)

Hi, I am a new learner of join command. Some result really make me confused. Please kindly help me. input: file1: LEO oracle engineer 210375 P.Jones Office Runner ID897 L.Clip Personl Chief ID982 S.Round UNIX admin ID6 file2: Dept2C ID897 6 years Dept5Z ID982 1 year Dept3S ID6 2... (1 Reply)
Discussion started by: summer_cherry
1 Replies

9. Shell Programming and Scripting

Join Command Help

Hi, I am trying to use join command for two files of size greater than 1 GB. join -t , -1 2 -2 1 -o 1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.10,1.11,1.12,1.13,1.14,1.15,1.16,1.17,1.18,1.19,1.20,1.21,1.22,1.23 File1 File2 > File3 we are facing space crunch after using these join command. May i... (0 Replies)
Discussion started by: jerome Sukumar
0 Replies

10. Shell Programming and Scripting

join command

Hi, I'm using the join command and it appears to discard certain fields. Here are the two files i'm comparing: File1: 1 a 2 b 3 c 4 d 99 f 101 g 999 i 200 j File 2: 1 e 2 f 3 g 4 h 99 h (22 Replies)
Discussion started by: penfold
22 Replies
Login or Register to Ask a Question