#!/bin/bash
# soundex.sh: Calculate "soundex" code for names
# ============================
# Soundex script
# by
# Mendel Cooper
# thegrendel@theriver.com
#
# Placed in the Public Domain.
# Used with Permission
# ============================
ARGCOUNT=1 # Need name as argument.
E_WRONGARGS=70
if [ $# -ne "$ARGCOUNT" ]
then
echo "Usage: `basename $0` name"
exit $E_WRONGARGS
fi
assign_value () # Assigns numerical value
{ #+ to letters of name.
val1=bfpv # 'b,f,p,v' = 1
val2=cgjkqsxz # 'c,g,j,k,q,s,x,z' = 2
val3=dt # 'd,t' = 3
val4=l # 'l' = 4
val5=mn # 'm,n' = 5
val6=r # 'r' = 6
# Exceptionally clever use of 'tr' follows.
value=$( echo "$1" \
| tr -d wh \
| tr $val1 1 | tr $val2 2 | tr $val3 3 \
| tr $val4 4 | tr $val5 5 | tr $val6 6 \
| tr -s 123456 \
| tr -d aeiouy )
# Assign letter values.
# Remove duplicate numbers, except when separated by vowels.
# Ignore vowels, except as separators, so delete them last.
# Ignore 'w' and 'h', even as separators, so delete them first.
#
# The above command substitution lays more pipe than a plumber .
}
input_name="$1"
echo
echo "Name = $input_name"
# Change all characters of name input to lowercase.
# ------------------------------------------------
name=$( echo $input_name | tr A-Z a-z )
# ------------------------------------------------
# Just in case argument to script is mixed case.
# Prefix of soundex code: first letter of name.
# --------------------------------------------
char_pos=0 # Initialize character position.
prefix0=${name:$char_pos:1}
prefix=`echo $prefix0 | tr a-z A-Z`
# Uppercase 1st letter of soundex.
let "char_pos += 1" # Bump character position to 2nd letter of name.
|