UNIX Join not working as expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX Join not working as expected
# 1  
Old 04-05-2016
UNIX Join not working as expected

Hello All,

I'm working on a Shell script to join data from two files using Join command but not able to get the desired output as its throwing me an error:
Quote:
join: file 2 is not in sorted order
Quote:
join: file 1 is not in sorted order
I have sorted the two files on the Column 1 numerically which is used as Join clause

File 1:
Code:
1,ABC,GGG,20160401
2,XYZ,KKK,20160401
3,LMN,TTT,20160401
4,OPQ,QQQ,20160401
5,STV,III,20160401
6,WXY,PPP,20160401
8,TTT,ARR,20160401
9,HHH,NBB,20160401
10,GGG,UYT,20160401
11,YYY,ADP,20160401
12,DDDD,WWR,20160401

Code:
cat a.op|sort -t"," -k1n,1 >a1

File 2:
Code:
1,01-APR-16 07.11.43.380674 PM,TGG,1
2,01-APR-16 07.40.44.223631 PM,YYH,2
3,04-APR-16 12.45.08.531580 AM,JUI,6
4,02-APR-16 01.36.40.508209 AM,TSH,3
5,02-APR-16 03.48.06.361722 AM,OPP,5
6,04-APR-16 03.03.02.873126 AM,RRT,13
7,04-APR-16 01.59.41.190489 AM,NHY,7
8,02-APR-16 01.54.24.063732 AM,QWERT,4
10,04-APR-16 02.16.19.239656 AM,AGH,8
11,04-APR-16 04.27.52.614581 AM,AHHT,14
12,04-APR-16 02.38.28.369315 AM,HJUI,10

Code:
cat b.op|sort -t"," -k1n,1 >b1

Below is the command used to join the two files a1 and b1

Code:
join -1 1 -2 1 -t, a1 b1

Below is the error I get :

Code:
-sh-4.1$ join -1 1 -2 1 -t, a1 b1
1,ABC,GGG,20160401,01-APR-16 07.11.43.380674 PM,TGG,1
2,XYZ,KKK,20160401,01-APR-16 07.40.44.223631 PM,YYH,2
3,LMN,TTT,20160401,04-APR-16 12.45.08.531580 AM,JUI,6
4,OPQ,QQQ,20160401,02-APR-16 01.36.40.508209 AM,TSH,3
5,STV,III,20160401,02-APR-16 03.48.06.361722 AM,OPP,5
6,WXY,PPP,20160401,04-APR-16 03.03.02.873126 AM,RRT,13
join: file 2 is not in sorted order
8,TTT,ARR,20160401,02-APR-16 01.54.24.063732 AM,QWERT,4
join: file 1 is not in sorted order

Is it because missing of the number 7 in file?

Can you please suggest whats the issue here?

Thanks in Advance


Moderator's Comments:
Mod Comment Please use code tags for data as well!

Last edited by Scrutinizer; 04-05-2016 at 04:38 PM.. Reason: Added code tags around data blocks
# 2  
Old 04-05-2016
Hi,

I think that you may have to modify your command to;

Code:
join -1 1 -2 1 -t, -a a1 -a b1 a1 b1

This is due to the fact that you have unmatched lines.

You may have to check man join just in case I'm having a senior moment.

Regards

Gull04
# 3  
Old 04-08-2016
Thanks for the inputs
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cp command not working as expected in HPUX

Hi, I'm having trouble with a simple copy command in a script on HPUX. I am trying to copy a file and append date & time. The echo command prints out what I am expecting.. echo "Backing up $file to $file.$DATE.$FIXNUM" | tee -a $LOGFILE + echo 'Backing up... (4 Replies)
Discussion started by: Glennyp
4 Replies

2. UNIX for Advanced & Expert Users

Test -e not working as expected (by me)

I ran into the following and still do not understand entirely the rationale behind this. If someone could explain why things are as they are I'd be thankful. The following was tested on AIX 7.1 with ksh88, but i suspect that to be ubiquitous. In an installation routine i had to create a set of... (6 Replies)
Discussion started by: bakunin
6 Replies

3. Shell Programming and Scripting

Script not working as expected

Hi, I have prepared a script and trying to execute it but not getting expected output. Could you please help and advise what is going wrong. "If else" part in below script is not working basically. I am running it on HP-UX. for i in slpd puma sfmdb do echo "******\t$i\t*******" echo... (10 Replies)
Discussion started by: sv0081493
10 Replies

4. UNIX for Dummies Questions & Answers

Nohup not working as expected

Hi. I am trying to start a script on my router that will execute even if i log off. To execute the script I write: nohup ./dslconnection > dslstat.out 2>&1 & It starts the job: 21968 admin 1604 S /bin/ash ./dslconnection The problem is that when I log back in the job has been... (6 Replies)
Discussion started by: sebcou
6 Replies

5. Shell Programming and Scripting

echo is not working as expected

for i in `cat /export/home/afahmed/Arrvial_time.txt` do echo $i echo $i | awk '$3 < $D { print $4 }' >> dynamic_DF.txt; done When i echo, its echo as Nov 15 02:24 /export/home/pp_adm/inbound//wwallet_20111115.txt where i expect it to be Nov 15 02:24... (7 Replies)
Discussion started by: afahmed
7 Replies

6. UNIX for Dummies Questions & Answers

-atime not working as expected

I need to sort through a volume that contains video files by access time and delete files that have not been accessed over x days. I have to use the access time as video files are originals that do not get modified, just read Testing commands on a local test folder... $ date Wed Sep 28... (10 Replies)
Discussion started by: canon273
10 Replies

7. Shell Programming and Scripting

Why this is not working in expected way?

total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $totalThis outputs: 1 3 6 10 15 0whereas I am expecting: 1 3 6 10 15 15My bash version: (4 Replies)
Discussion started by: meharo
4 Replies

8. UNIX for Dummies Questions & Answers

Redirection not working as expected

Portion of my script below : if ; then NUMBEROFFEILDS=`cat ${BASE_SCRIPT_LOC}/standardfilecleanup.lst|grep -w ${db_file_path}|awk -F: '{print NF}'` COUNT=4 while ; do awk_var="$"`echo $COUNT` file_name1=`cat ${BASE_SCRIPT_LOC}/standardfilecleanup.lst|grep -w... (1 Reply)
Discussion started by: findprakash
1 Replies

9. Shell Programming and Scripting

ls not working as expected within ksh

Hi, I use the command ls a\b\c\*.txt from the command line on HP UNIX and it works fine - It lists all files matching *.txt in the a\b\c directory When embeded in a ksh script `ls a\b\c\*.txt` it does not work - I get *.txt not found (even though there are files) I tried... (10 Replies)
Discussion started by: GNMIKE
10 Replies

10. Shell Programming and Scripting

which not working as expected

Hello. Consider the following magic words: # ls `which adduser` ls: /usr/sbin/adduser: No such file or directory # Hmmm... Then: # ls /usr/sbin/adduser /usr/sbin/adduser # Now what? Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)
Discussion started by: osee
2 Replies
Login or Register to Ask a Question