The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
join two files koti_rama Shell Programming and Scripting 5 08-05-2008 01:20 AM
awk, join or sed jkl_jkl Shell Programming and Scripting 1 04-15-2008 02:55 AM
join (pls help on join command) summer_cherry Shell Programming and Scripting 1 12-31-2007 02:19 AM
Join help with -e option Ken555 UNIX for Dummies Questions & Answers 2 08-21-2006 07:50 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-24-2008
Registered User
 

Join Date: Nov 2007
Posts: 20
Join

Hi,
Is there a way to get join to join 2 files even if in one file one of the records does not exist?
i.e. 1st file
aaa 50
bbb 60
2nd file
aaa 40

So i want to join the 2 files together and get the following output:
aaa 50 aaa 40
bbb 60 bbb 0

Is this possible???
Thanks
Jason
Reply With Quote
Forum Sponsor
  #2  
Old 01-26-2008
Registered User
 

Join Date: Feb 2007
Posts: 51
join

For successfule join commd execution
(1) sort both the file in same order the field on which you want to join
(2) the length must be same

use the commd: join file1 file2 > o/p file name
Reply With Quote
  #3  
Old 01-27-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 546
Hi.

On some versions of join, the lengths need not be the same. This may not result in the exact output you want, but non-matching lines can be included:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate feature.

#  ____
# /
# |   Infrastructure BEGIN

set -o nounset
echo

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
uname -rs
version >/dev/null 2>&1 && version bash join

echo

FILE1=${1-data1}
echo " Input file $FILE1:"
cat $FILE1

echo
FILE2=${1-data2}
echo " Input file $FILE2:"
cat $FILE2

# |   Infrastructure END
# \
#  ---

echo
echo " Results from processing:"
join -a 1 -e 0 $FILE1 $FILE2

exit 0
Producing:
Code:
$ ./s1

(Versions displayed with local utility "version")
SunOS 5.10
GNU bash 3.00.16
join (local) - no version provided.

 Input file data1:
aaa 50
bbb 60

 Input file data2:
aaa 40

 Results from processing:
aaa 50 40
bbb 60
See man pages for details ... cheers, drl
Reply With Quote
  #4  
Old 01-28-2008
Registered User
 

Join Date: Feb 2007
Posts: 51
join

Thanks drl for throwing the lights on the subject , but as far as SCO-UNIX,
UNIX-VER5 are concerned I have experimented and found that if the length
is not equal of the field on which you wanted to join , it fails , same on
RedHat-9 too. Correct me if I am wrong , b'Coz on many occassion I have
seen if the file say if you want to join on strings and the same is not
sorted on -d order or digit not sorted on -n order and the likewise it gave me NIL output. But would like to have your suggestion too . Alwasy ready
to LEARN new things. Thanx in advance to discuss on the subject.
Reply With Quote
  #5  
Old 01-28-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 546
Hi, vakharia Mahesh.

I think we may be talking about different lengths. I was talking about the length of the files which might be different if it is the case that "one of the records does not exist" as the OP wrote.

I agree that the fields need to be the same length in characters, and the files need to be sorted on the fields to be joined. If the fields are not the same length, then one would need to do some pre-and-post processing to add extra fields, join, remove extra fields. One could also create an awk or perl script for such special cases.

Best wishes ... cheers, drl
Reply With Quote
  #6  
Old 01-29-2008
Registered User
 

Join Date: Feb 2007
Posts: 51
join

Thanx drl for your response on the subject and removing the doubt
and as far as the length of file is concerned it does not matter for
successful operation of join commd.

Thanks once again D R L c u
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0