#! /bin/csh -f 

# Reads a file of word counts, as produced by "uniq -c"
# Outputs a similar file where the counts are followed by 
# the corresponding fractions relative to the total count.

set temp = "/tmp/$$"
set addpgm = '/^#/ {next} /./ {S+=$1; if(NF!=2){print "bad word">>"/dev/stderr";exit 1}} END {print S}'

cat > ${temp}

set total = "`cat ${temp} | gawk '${addpgm}'`"
if ("x${total}" == "x") set total = "1"

cat ${temp} \
  | gawk \
      '/^#/ {print} /./ {V=$1; $1=""; printf "%7d %7.5f%s\n", V, (V/'"${total}"'), $0}'
      
/bin/rm -f ${temp}