#!/bin/sh # /******************* self documentation **********************/ DOC () { echo ' REF - search references from BIB files in $BIBINPUTS Usage: ref keyword(s) [options] Optional Parameters: case=0, =1, make it sensitive to case in keyword(s) Here "keyword(s)" are parts of author names, titles, and any others. The command will print out sets of "references" which match every keyword you choose.' 1>&2 } # /**************** end self doc ********************************/ # Author: Seongjai Kim; 10-25-96 ; modified: Jan-20-97, Oct-14-97 TKEYS= icase=0 case $# in 0) DOC; exit 1 ;; *) for i do case $i in case=*) icase=`echo $i | sed 's/case=//'` ;; *) TKEYS="$TKEYS $i" ;; esac done ;; esac #------------------------------------- # test if "BIBINPUTS" is defined #------------------------------------- if test "${BIBINPUTS}" = ""; then echo " The environment variable \"BIBINPUTS\" is not set. Set e.g. setenv BIBINPUTS \".:/home/chaoming/pub\" " exit 1 fi #------------------------------------- # make the keywords not sensitive. #------------------------------------- if test $icase -eq 0; then KEYS="`echo $TKEYS | awk '{ for(j=1;j<=NF;j++){ leng=length($j) newword="" for(i=1;i<=leng;i++){ alett=substr($j,i,1) ind=index("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",alett) if(ind==0) newword= newword alett else{ add=substr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",ind,1) newword= newword "[" alett add "]" } } $j=newword } printf("%s\n",$0) }'`" else KEYS="$TKEYS" fi nkey=`echo $TKEYS | wc -w` #------------------------------------- # check BIB files #------------------------------------- REF_DIR="`echo $BIBINPUTS | sed s/\:/\ /g`" OBJS= tnf=0 for dir in $REF_DIR; do nf=`ls $dir | awk '$1 ~ /\.bib$/{print}' | wc -w` tnf=`expr $tnf + $nf` if test $nf -gt 0 ; then ONE_DIR=$dir/'*.bib' for bibfile in $ONE_DIR; do OBJS="$OBJS $bibfile" done fi done if test $tnf = 0 ; then echo ' No *.bib files in $BIBINPUTS..' exit else echo " Searching references including '$TKEYS'" echo " from the following $tnf BIB file(s):" echo "" fi #------------------------------------- # make a temporary awk file #------------------------------------- TEMP_AWK="$HOME/0000tempfile" echo '{line[++ns] = $0}' > $TEMP_AWK count=1 for word in $KEYS; do echo "/$word/{chk$count++;if(chk$count<=1)ok++}">>$TEMP_AWK count=`expr $count + 1` done echo '{if(NF==0 || ($1~/^@/ && FmNF!=0)){' >> $TEMP_AWK echo " if(ok==$nkey){nset++" >> $TEMP_AWK echo ' n1=NR-ns+1; n2=NR-1 printf("%s %s %s %s",":::: Lines",n1,"-",n2) print "" for(i=1;i<=(ns-1);i++) print line[i] print ""} ok=0' >> $TEMP_AWK count=1 for word in $KEYS; do echo " chk$count=0" >> $TEMP_AWK count=`expr $count + 1` done echo ' if($1~/^@/){line[1]=line[ns]; ns=1'>> $TEMP_AWK count=1 for word in $KEYS; do echo " if(\$0~/$word/){chk$count++;if(chk$count<=1)ok++}">>$TEMP_AWK count=`expr $count + 1` done echo " } else{ns=0} } } {FmNF=NF} END{ if (ok==$nkey){nset++" >> $TEMP_AWK echo ' n1=NR-ns+1; n2=NR printf("%s %s %s %s",":::: Lines",n1,"-",n2) print "" for(i=1;i<=ns;i++) print line[i] print "" } if(nset==0) {printf(" %s","No match in the file"); print ""; print ""} }' >> $TEMP_AWK #------------------------------------- # now, search all words needed #------------------------------------- for file in $OBJS; do echo "#### $file" awk -f $TEMP_AWK $file done rm -f $TEMP_AWK