Join


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Join
# 1  
Old 01-24-2008
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
# 2  
Old 01-27-2008
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
Smilie
# 3  
Old 01-27-2008
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
# 4  
Old 01-28-2008
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.Smilie
# 5  
Old 01-28-2008
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
# 6  
Old 01-29-2008
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
Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
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. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

5. UNIX for Dummies Questions & Answers

Use of join

I want basically to merge two config files each of them look like that File:Option1 value1 Optionx valuex .... One of those files is default config while the other is generated by my script. Now here is the problem, when my script generates an option that previously has been set by default i... (4 Replies)
Discussion started by: Adas
4 Replies

6. Shell Programming and Scripting

Join

joining two files: File1: ----- 1|M 2|M 3|F File2: ----- 1|abc|def 3|xyz|pqr join -t '|' 1.txt 2.txt gives(Itried many other ways) 1|M|abc|def 3|F|xyz|pqr but I need the result like following (14 Replies)
Discussion started by: greenworld
14 Replies

7. Programming

sql,multiple join,outer join issue

example sql: select a.a1,b.b1,c.c1,d.d1,e.e1 from a left outer join b on a.x=b.x left outer join c on b.y=c.y left outer join d on d.z=a.z inner join a.t=e.t I know how single outer or inner join works in sql. But I don't really understand when there are multiple of them. can... (0 Replies)
Discussion started by: robbiezr
0 Replies

8. Shell Programming and Scripting

Join

Hi, need some help in joining please, FILE 1: ------- 1|05/20/2009| 2|04/21/2009| 2|03/21/2009| FILE 2: ------- 1|Michel|Hawkins|05/10/2009| 1|Michel|Hawkins|03/10/2007| 2|Krish|Lander|09/10/2005| FILE 3: ------- 1|M|32|03/22/2009| 2|M|42|04/22/2009| 2|M|41|03/10/2008| I want to... (7 Replies)
Discussion started by: greenworld
7 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
Login or Register to Ask a Question