
# UNISRC_ID: @(#)countem.sh	27.2	84/12/03  
# Script to do a common counting operation from standard input or a named file.

# Usage: <script> [-p] [filenames...]
# Counts the numbers of occurrences of each type of line in input, and shows
# the counts sorted by count, descending then line, ascending, with a total.
# With -p option, adds percentages.


# Check arguments:

	if [ x"$1" = x-p ]
	then
		pflag=1
		shift
	else
		pflag=0
	fi


# Build raw data lines:

	sort $*		|		# might read stdin.
	uniq -c		|		# count by type.
	sort +0nr -1	|		# count descending, data ascending.

# Add percentages (optional) and total:

	if [ $pflag = 0 ]		# no percentages:
	then
	    awk '
	        { print; total += $1;	  }
	    END { printf ("%4d\n", total) }
	    '
	else				# with percentages:
	    awk '
		{
		    count [NR] = $1;
		    line  [NR] = $0;
		    total     += $1;
		}

	    END	{
		    if (total)	safetot = total;
		    else	safetot = 1;

		    for (ln = 1; ln <= NR; ln++)
		    {
			cum += count [ln];
			printf ("%4.0f%4.0f%s\n",		\
				count [ln] * 100.0 / safetot,	\
				cum * 100.0 / safetot, line [ln]);
		    }
		    printf (" 100 100 %3d\n", total);
	    }'
	fi
