2005-03-21 13:52:30 +00:00
|
|
|
#! /bin/sh
|
|
|
|
# letter-count2.sh: Counting letter occurrences in a text file.
|
|
|
|
#
|
|
|
|
# Script by nyal [nyal@voila.fr].
|
2006-12-20 21:11:55 +00:00
|
|
|
# Used in ABS Guide with permission.
|
|
|
|
# Recommented and reformatted by ABS Guide author.
|
2005-03-21 13:52:30 +00:00
|
|
|
# Version 1.1: Modified to work with gawk 3.1.3.
|
|
|
|
# (Will still work with earlier versions.)
|
|
|
|
|
|
|
|
|
|
|
|
INIT_TAB_AWK=""
|
|
|
|
# Parameter to initialize awk script.
|
|
|
|
count_case=0
|
|
|
|
FILE_PARSE=$1
|
|
|
|
|
2008-11-23 22:43:47 +00:00
|
|
|
E_PARAMERR=85
|
2005-03-21 13:52:30 +00:00
|
|
|
|
|
|
|
usage()
|
|
|
|
{
|
2014-07-05 13:38:40 +00:00
|
|
|
echo "Usage: letter-count.sh file letters" 2>&1
|
2005-03-21 13:52:30 +00:00
|
|
|
# For example: ./letter-count2.sh filename.txt a b c
|
2006-12-20 21:11:55 +00:00
|
|
|
exit $E_PARAMERR # Too few arguments passed to script.
|
2005-03-21 13:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ ! -f "$1" ] ; then
|
2014-07-05 13:38:40 +00:00
|
|
|
echo "$1: No such file." 2>&1
|
2005-03-21 13:52:30 +00:00
|
|
|
usage # Print usage message and exit.
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$2" ] ; then
|
2014-07-05 13:38:40 +00:00
|
|
|
echo "$2: No letters specified." 2>&1
|
2005-03-21 13:52:30 +00:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
shift # Letters specified.
|
|
|
|
for letter in `echo $@` # For each one . . .
|
|
|
|
do
|
2006-12-20 21:11:55 +00:00
|
|
|
INIT_TAB_AWK="$INIT_TAB_AWK tab_search[${count_case}] = \
|
|
|
|
\"$letter\"; final_tab[${count_case}] = 0; "
|
2005-03-21 13:52:30 +00:00
|
|
|
# Pass as parameter to awk script below.
|
|
|
|
count_case=`expr $count_case + 1`
|
|
|
|
done
|
|
|
|
|
|
|
|
# DEBUG:
|
|
|
|
# echo $INIT_TAB_AWK;
|
|
|
|
|
|
|
|
cat $FILE_PARSE |
|
|
|
|
# Pipe the target file to the following awk script.
|
|
|
|
|
2006-12-20 21:11:55 +00:00
|
|
|
# ---------------------------------------------------------------------
|
|
|
|
# Earlier version of script:
|
|
|
|
# awk -v tab_search=0 -v final_tab=0 -v tab=0 -v \
|
|
|
|
# nb_letter=0 -v chara=0 -v chara2=0 \
|
2005-03-21 13:52:30 +00:00
|
|
|
|
|
|
|
awk \
|
|
|
|
"BEGIN { $INIT_TAB_AWK } \
|
|
|
|
{ split(\$0, tab, \"\"); \
|
|
|
|
for (chara in tab) \
|
|
|
|
{ for (chara2 in tab_search) \
|
|
|
|
{ if (tab_search[chara2] == tab[chara]) { final_tab[chara2]++ } } } } \
|
|
|
|
END { for (chara in final_tab) \
|
|
|
|
{ print tab_search[chara] \" => \" final_tab[chara] } }"
|
2006-12-20 21:11:55 +00:00
|
|
|
# ---------------------------------------------------------------------
|
2005-03-21 13:52:30 +00:00
|
|
|
# Nothing all that complicated, just . . .
|
|
|
|
#+ for-loops, if-tests, and a couple of specialized functions.
|
|
|
|
|
|
|
|
exit $?
|
|
|
|
|
|
|
|
# Compare this script to letter-count.sh.
|