#! /bin/bash
# words fequence counting and format with a chart
# CYGWIN_NT-6.0 User-PC 1.5.24,GNU bash version 3.2.17
# Jun 30, 2007 twf_cc@yahoo.com.hk
# public domain
# init some stuff
tempfile=$HOME/junk1.$$
tempfile2=$HOME/junk2.$$
msg="abort by user..exiting"
msg2="Usage: $0 [option][ -c --chart | -n --number ] file"
msg3="file not found"
if [ $# -eq 0 ] ; then
echo "$msg2" >&2
exit 1
elif [ $# -eq 1 ] ; then
file=$1
elif [ $# -eq 2 ] ; then
option=$1
file=$2
else
option=$1
file=$2
fi
# make something to avoid remaining temp file
# when an accident is happened
trap "echo $msg >&2 ; rm -f $tempfile $tempfile2 ; exit 1" 1 2 15
# ok , make a temp file, sorting and counting its word
cat "$file" | tr -s ' ' '\n' | sort | uniq -c | sort -rn > $tempfile
# make something to the data
jpg='#'
while read -r num word
do
echo -ne "$num\t$word\t"
printf '%*s\n' $num | tr " " "$jpg"
done < $tempfile > $tempfile2
# format the output
case "$option" in
-c| --chart) awk '{
printf("%-20s%-30s\n", $2, $3)
}' $tempfile2
;;