#!/bin/sh
#
# Shell script to output the N most frequent words
# from a specified text file.
#
# Syntax:  most-frequent [-N] filename
#
# Author: Bill Slough
#  

# Make sure at least one argument appears
if [ $# -eq 0 ]
then
    echo "Usage: most-frequent [-N] filename"
    exit 1
fi

# Is the first argument a switch?
# To find out, inspect the first character of the first argument
switch=${1:0:1}
if [ "$switch" = "-" ]
then
  # Record the desired number of words to find
  HowMany=$1
  shift
else
  # Set the default number of words to find
  HowMany=-5
fi

# Verify the correct number of parameters were given
if [ $# -ne 1 ]
then
   # Incorrect number of parameters; complain and exit
   echo "Usage: most-frequent [-N] filename"
   exit 1
fi

# The correct number of parameters were given; make sure the
# specified file exists
if [ ! -f $1 ]
then
  # File doesn't exist; complain and exit
  echo "most-frequent: $1 does not exist."
  exit 1
fi

# Everything looks good; do the work!
cat $1 | 
tr 'A-Z' 'a-z' | 
tr -sc 'a-zA-Z' '\n' | 
sort | 
uniq -c | 
sort -n | 
tail $HowMany

# Report success
exit 0