# # A version of join not requiring the files to be sorted # if [ "$1" = "" ] || [ "$1" = "-?" ] || [ "$1" = "--help" ] then echo "Usage: join.unsorted [OPTION]... FILE1 FILE2" echo "For each pair of input lines with identical join fields, write a line to" echo "standard output. The default join field is the first, delimited" echo "by whitespace. When FILE1 or FILE2 (not both) is -, read standard input." echo "" echo " -a FILENUM print unpairable lines coming from file FILENUM, where" echo " FILENUM is 1 or 2, corresponding to FILE1 or FILE2" echo " -e EMPTY replace missing input fields with EMPTY" echo " -i, --ignore-case ignore differences in case when comparing fields" echo " -j FIELD equivalent to ""-1 FIELD -2 FIELD""" echo " -o FORMAT obey FORMAT while constructing output line" echo " -t CHAR use CHAR as input and output field separator" echo " -v FILENUM like -a FILENUM, but suppress joined output lines" echo " -1 FIELD join on this FIELD of file 1" echo " -2 FIELD join on this FIELD of file 2" echo " --help display this help and exit" echo " --version output version information and exit" echo "" echo "Unless -t CHAR is given, leading blanks separate fields and are ignored," echo "else fields are separated by CHAR. Any FIELD is a field number counted" echo "from 1. FORMAT is one or more comma or blank separated specifications," echo "each being FILENUM.FIELD or 0. Default FORMAT outputs the join field," echo "the remaining fields from FILE1, the remaining fields from FILE2, all" echo "separated by CHAR." echo "" exit fi AWK=gawk AOPTION=0 EMPTY="" FIELD1=1 FIELD2=1 FIELDSEP=" " IGNORECASE=0 VOPTION=0 switch=1 while [ $switch = 1 ] do case "$1" in -a) AOPTION=$2; shift 2 ;; -1) FIELD1=$2; shift 2 ;; -2) FIELD2=$2; shift 2 ;; -i) IGNORECASE=$2; shift 2 ;; -j) FIELD1=$2; FIELD2=$2; shift 2 ;; -t) FIELDSEP=$2; shift 2 ;; -v) VOPTION=$2; shift 2 ;; -*) echo "Flag not recognised: $1" ; shift ;; *) switch=0 ;; esac done if ! [ -f $1 ] || ! [ -f $2 ] then echo "join.unsorted: too few non-option arguments" exit fi FIL1=$1 FIL2=$2 if [ $AOPTION = 0 ] || [ $AOPTION = 1 ] then $AWK -v aoption=$AOPTION -v empty=$EMPTY -v field1=$FIELD1 -v field2=$FIELD2 \ -v voption=$VOPTION -v fil2=$FIL2 -v fieldsep=$FIELDSEP ' BEGIN { if (length(fieldsep)>0) { FS=fieldsep OFS=fieldsep } while(getline < fil2) { idx=$(field2) $(field2)="" lin[idx]=$0 } } { idx=$(field1) $(field1)="" if (idx in lin) { print idx, $0, lin[idx] delete lin[idx] }else if (aoption==1) { print idx, $0, empty } }' $FIL1 else $AWK -v aoption=$AOPTION -v field1=$FIELD1 -v field2=$FIELD2 \ -v voption=$VOPTION -v fil1=$FIL1 -v fieldsep=$FIELDSEP ' BEGIN { FS=fieldsep OFS=fieldsep while(getline < fil1) { idx=$(field1) $(field1)="" lin[idx]=$0 } } { idx=$(field2) $(field2)="" if (idx in lin) { print idx, lin[idx], $0 delete lin[idx] }else if (aoption==2) { print idx, empty, $0 } }' $FIL2 fi