Skip to content

Instantly share code, notes, and snippets.

@paddy74
Last active December 24, 2017 00:41
Show Gist options
  • Save paddy74/f05592e50261c79620a7ddff57279600 to your computer and use it in GitHub Desktop.
Save paddy74/f05592e50261c79620a7ddff57279600 to your computer and use it in GitHub Desktop.
Count the number of words, sentences, and average number of words per sentence in a text file. Created for CS252 at Old Dominion University.
#!/bin/sh
for f in "$@"
do
w=`cat "$f" | sed "s/ */\\n/g" | sed "s/[^a-zA-Z0-9]//g" | sed "s/[0-9]*//g" | sed "/^$/d" | wc -w`
s=`cat "$f" | tr -d " \t\n\r" | sed "s/\./\\n/g" | sed "s/\?/\\n/g" | sed "s/\!/\\n/g" | wc -l`
z=$(expr "$w" / "$s")
echo $f:
echo " "$w words
echo " "$s sentences
echo " "The average sentence is $z words long.
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment