join command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting join command
# 8  
Old 02-21-2005
Sure thing:

sales is the initial sales file
associates is the associates file
SRep is the script

sales2 and assoc are the same files hand typed with the ":" separator. I don't/haven't tried that with the script - but instead run a manual join and they work fine.

Thanks for the help
umm - I selected upload, but the files did not "Attach" hmm well here is a cut and paste of the files:

sales (partial):
104,2,03:07:2004,24
104,3,03:07:2004,24
113,92,12:07:2004,24
104,2,03:07:2004,24
104,1,03:07:2004,26
104,2,03:07:2004,24
112,1,01:07:2004,23
104,9,03:08:2004,21
105,2,03:08:2004,24
111,4,12:07:2004,21
106,2,03:07:2004,24
107,9,03:08:2004,21
108,21,03:07:2004,22
109,12,03:08:2004,24
110,2,03:08:2004,23
104,2,03:07:2004,24
104,2,03:09:2004,24
104,1,03:09:2004,26
104,2,03:09:2004,24
112,1,01:09:2004,23
104,9,03:09:2004,21
104,2,05:09:2004,24
104,1,05:09:2004,26
104,2,05:09:2004,24
112,1,05:09:2004,23
104,9,05:09:2004,21

Associates:
21/John Doe/39000/Clerk
22/Kirk Russell/99000/President
23/Susan Smith/44000/Manager
24/Fast Buck/21000/Stock Boy
25/Scott Brown/99000/Engineer
26/Aimee Woods/88000/Secretary

script SRep:
#!/bin/bash
#=============================================================
# Script: Sales Report - Assignment 2
# Date: February 2005
# Purpose:

#trap " rm t[1] 2>/dev/null; exit" 0

awk '
BEGIN {
{ FS = ":"}
{ print "\t Marine Parts R US\n" }
{ print "\t Sales Associates Ranking\n" }
{ "date" | getline d }
{ printf "\t %s\n",d }
}'

# Select the September sales
awk '/:09:/ { print $0 }' sales > sept
echo "============== September Sales ============"
# cat sept

# Strip the dates since we don't need them anymore
# the sort the file on the ID field
cut -d, -f1,2,4 sept > sept_wo_dates
sort -t, -k 3 < sept_wo_dates -o sept_sort
# cat sept_sort

# make the sales file have a : separator
sed -n 's/,/:/gp' < sept_sort > sept_colon
cat sept_colon
# make the associates file have a consistent : seperator
sed -n 's/[/]/:/gp' < associates > assoc_colon
cat assoc_colon

# join the two files
join -t: -1 3 -2 1 -o 1.1 -o 1.2 -o 2.2 -o 2.4 sept_colon assoc_colon > sept_assoc
cat sept_assoc

the hand made files:
sales2 (partial)
109:1:03:09:2004:24
104:2:03:07:2004:24
104:3:03:07:2004:24
113:92:12:07:2004:24
104:2:03:07:2004:24
104:1:03:07:2004:26
104:2:03:07:2004:24
112:1:01:07:2004:23
104:9:03:08:2004:21
105:2:03:08:2004:24
111:4:12:07:2004:21
106:2:03:07:2004:24
107:9:03:08:2004:21
108:21:03:07:2004:22

assoc:
21:John Doe:39000:Clerk
22:Kirk Russell:99000:President
23:Susan Smith:44000:Manager
24:Fast Buck:21000:Stock Boy
25:Scott Brown:99000:Engineer
26:Aimee Woods:88000:Secretary

Last edited by codpilot; 02-21-2005 at 12:32 PM..
# 9  
Old 02-21-2005
Quote:
Originally Posted by codpilot
Sure thing:

sales is the initial sales file
associates is the associates file
SRep is the script

sales2 and assoc are the same files hand typed with the ":" separator. I don't/haven't tried that with the script - but instead run a manual join and they work fine.

Thanks for the help
well..... usually have sample inputs helps to figure out the problem, but... given the blow:

sales.txt
Code:
15:joe:5000:engineer

and

assoc.txt
Code:
20:75000:15

and running:
Code:
join -t: -1 1 -2 3 sales.txt assoc.txt

produces:
Quote:
15:joe:5000:engineer:20:75000
# 10  
Old 02-21-2005
Sorry I tried to upload the files and that didn't work for some reason, so I edited it with sample input.

You are correct, that works fine. When I create the files with a common separator its perfect, when I use sed to make the separators match - nothing, nada zip. Heck I simply retype the output of the sed command with pico and it works. Rather frustrating. grrr
# 11  
Old 02-21-2005
Here's a hint for you to analyze your data AFTER you sed your file.
here's your sale file BEFOREsed-ing:
Quote:
104,2,03:07:2004,24
what field is your key?

here's your file AFTER sed-ing:
Quote:
104:2:03:07:2004:24
what field is your key NOW?

Hint:
already have fields with embedded ':'. Use different separator when sed-ing so that your key fields from both files saty in their respective places - something like '#'.
# 12  
Old 02-21-2005
Even with that, they still won't join. I just modded the script. As it stood I was stripping out the date field that had the same field separator (with the cut command) so the field position in the join was already determined.
# 13  
Old 02-21-2005
I think you got your command line order incorrect/swapped.
I think it should be :
Code:
join -t: -1 1 -2 3 assoc sales

# 14  
Old 02-21-2005
In the script it's correct - my typing may be discombobulated though. The whole objective is to take a file, change the separators then join them together. Apparently sed does something so that when you do a replace - it doesn't quite make the grade anymore.

As a matter of fact if you type in a line with the "/" separator then sed it to ";", create a second file with the same line already using a ";" they will not match (try it using comm). I guess the question is why? What is it adding? ah well.

This will work with one line but not with multiple (or at least not consistently)

Last edited by codpilot; 02-21-2005 at 01:47 PM..
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

using join command

I am given: Sara:Smith:11234:3:63498:25:40 Perry:Potter:12445:2:35664:15:40 Ann:Abbott:23323:2:54865:22:42 Maple:Myers:24223:1:63498:18:35 Harold:Hanson:35664:2:54865:10:40 Bob:Brown:40778:1:24223:15:40 Jane:Jones:41288:1:53498:24:40 Wanda:Wallace:51122:4:63498:55:40... (4 Replies)
Discussion started by: ehshi1992
4 Replies

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question