The UNIX and Linux Forums  

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



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 05-07-2008
drl's Avatar
drl drl is online now
Registered User
 

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

Here is a script that produces results for files not sorted, then sorted:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate join.

#  ____
# /
# |   Infrastructure BEGIN

echo
set -o nounset

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.

set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) sort join
set -o nounset

echo

FILE1=data1
echo " Input file $FILE1:"
cat $FILE1

echo
FILE2=data2
echo " Input file $FILE2:"
cat $FILE2

echo
echo " Results expected:"
cat expected-results

# |   Infrastructure END
# \
#  ---

echo
echo " Results from processing without a sort:"
join $FILE1 $FILE2

echo
echo " Results from processing after sorting files:"
sort $FILE1 >t1
sort $FILE2 >t2
join t1 t2

exit 0
Yielding:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
sort (coreutils) 5.2.1
join (coreutils) 5.2.1

 Input file data1:
rs10051507 5 q21.3
rs10051514 5 p15.32
rs10051527 5 q21.2
rs1005152 7 q21.3
rs10051540 5 q21.3
rs10051548 5 q21.1
rs1005155 X q27.3
rs10051594 5 q34

 Input file data2:
rs1003456 3
rs1005152 3

 Results expected:
rs1005152 7 q21.3 3

 Results from processing without a sort:

 Results from processing after sorting files:
rs1005152 7 q21.3 3
cheers, drl
Reply With Quote