Tag Archives: script

Simple single letter spell checker in bash

Here’s a small bash script. It checks if the given word is in dictionary and if not, assuming that it contains only a single character error, gives suggestions.

#!/bin/bash
word=$1
grep -i "^$word$" /usr/share/dict/words > /dev/null
if [ $? -eq 0 ]; then
   echo "Word in dictionary";
else
    echo "Word not in dictionary. Suggestions:"
    l=${#word}
    for i in $(seq 1 $l);
    do
        regw=`echo $word | sed s/././$i`
        grep -i "^$regw$" /usr/share/dict/words
    done
fi

This was actually a question i had  in Tux of War, a shell scripting competition organized as a part of tathva at NIT Calicut. I got the sed expression from stackoveflow. It replaces just one letter of the word with ‘.’ in order in aloop, so that it would be a valid regex in grep to find the word suggestions.

I’m happy that my attempt works and I’m also sure there must be an efficient and easy method to do this! 😀