ÈVUT FEL - X36UNX homework #13
Script write sorted list of input words, words quantity and line numbers of occurance (cross references)..
#!/bin/bash
# X36UNX CVUT FEL, homework #13
# script by Jan Cermak, [cermaj5 at fel.cvut.cz]
# Summer 2006

TMP_FILE=/tmp/uloha13_$$
TMP_USED=0	#is temp file used?
VERBOSE=0
LOWERCASE=0
FIELD_SEP='	'
declare -a slova

function usage () {
	echo "Usage:"
	echo "$0 [-vlh] [-s FS] [-f FILE]"
	echo
	echo " -?	this help"
	echo " -h	this help"
	echo " -l	convert to lowercase (case insensitive), default OFF"
	echo " -v	verbose output, default OFF"
	echo " -s FS	FS is separating character, default TAB"
	echo "if not used this parameter:"
	echo " -f FILE	file with input text"
	echo "then script reads standard input"
	exit 1
}

function programy() {		#basic required util control
	for prog in $*; do
		$prog --help >/dev/null 2>/dev/null
		if [ "$?" -ne 0 ]; then
			echo "Missing \`$prog\`"
			if [ "$VERBOSE" = 1 ]; then 
				echo "This utility is needed by script. Install it, or set PATH correctly"
			fi
			exit 2
		fi
	done
}

while getopts "?hlvf:s:" VOLBA
	do
		case $VOLBA in
		f)	FILE=$OPTARG;;
		l)	LOWERCASE=1;;
		v)	VERBOSE=1;;
		s)	FIELD_SEP=$OPTARG;;
		\?)	usage;;
		h)	usage;;
		esac
done

#if dont want to control these programs, comment line with #
programy wc grep seq head tail tr sed sort

if [ -z "$FILE" ]; then	#file not known - read std. in
	if [ "$VERBOSE" -eq 1 ]; then echo "#	temporary file $TMP_FILE is being used"; fi
	cat > $TMP_FILE
	FILE=$TMP_FILE
	TMP_USED=1
fi

if [ ! -z "$FILE" ]; then
	if [ ! -f "$FILE" ]; then echo "input file $FILE not exist!"; exit 1; fi
	if [ ! -r "$FILE" ]; then echo "could not read $FILE (no rights)"; exit 1; fi
fi

if [ ! -z "$FILE" ]; then	#is there input file?
	RADEK=`grep '' -n "$FILE" | wc -l`
	for i in `seq "$RADEK"`; do
		cmd=`head -$i "$FILE" | tail -1 | sed 's/[[:punct:]]/ /g'`
		if [ "$LOWERCASE" = 1 ]; then
			cmd=`echo $cmd | tr '[:upper:]' '[:lower:]'`
		fi
		for j in $cmd; do
			declare POC_$j=$(( POC_$j+1 ))
			#increase reference counter and add word into word list (slova == words ;-) )
			if [ $(( POC_$j )) -lt 2 ]; then slova+=($j); fi
			
			POMOC=LINE_$j
			STARE=`eval echo \\$$POMOC`
			eval $POMOC=\"$STARE $i\"
		done
	done
	
	if [ "$VERBOSE" -eq 1 ]; then echo "WORD${FIELD_SEP}COUNT${FIELD_SEP}LINES"; fi
	SERAD=`echo ${slova[*]} |sed 's/ /\n/g' | sort`
	for i in $SERAD; do
		POMOC=LINE_$i
		echo ${i}"${FIELD_SEP}"$(( POC_$i ))"${FIELD_SEP}"`eval echo \\$$POMOC`
	done
	
	if [ "$TMP_USED" -eq 1 ]; then
		rm "$TMP_FILE"
		if [ "$VERBOSE" -eq 1 ]; then echo "#	temp file $TMP_FILE deleted"; fi
	fi
fi

exit 0