I have a file of names, and I want to search within it, not caring too much about whether I have spelled the name ( that I am searching for ) correctly. I know that grep has quite a bit of functionality to search for a whole slew of similar strings within a file or stream, but as far as I am aware, it does not have functionality to correct for spelling errors, and even if it did, since these are names of people, they wouldn't be found inside a standard dictionary.

Perhaps I can make my file of names into a special dictionary, and then use some standard spell checking tool? Of particular importance in this application is the ability to match similarly sounding words.

For example: "jacob" should return "Jakob". Even better would be if inter-language similarities were also accounted for, so that "miguel" should match "Michael".

Is this something that has been implemented already, or will I have to build my own?

  • 3
    agrep for approximate grep (not for sound/language). Also in zsh pattern matching (#a3) for allow up to 3 mistakes. Jun 14, 2013 at 9:36
  • 7
    Take a look at the Text::Soundex perl core module too: pastebin.com/UbeVFBQA
    – manatwork
    Jun 14, 2013 at 10:40
  • 1
    @manatwork - you should write that up as an answer!
    – slm
    Jun 14, 2013 at 14:01
  • 2
    @slm, not sure how useful that can be in practice. For example “miguel”, “Michael”, “michelle”, “majkul” and “mysql” all have soundex code “M240”. When I tried to use it, I discovered that its too broad to be useful for most tasks. So I better let someone capable to fine tune it to make it an answer.
    – manatwork
    Jun 14, 2013 at 14:17

1 Answer 1


@manatwork has it right, soundex is probably the tool you're looking for.

Install the perl Soundex module using CPAN:

$ sudo cpan Text::Soundex
CPAN: Storable loaded ok (v2.27)
Text::Soundex is up to date (3.04).

Make a file full of names to test called names.txt


Now the perl script to use the Soundex module, soundslike.pl


use Text::Soundex;

open(FH, 'names.txt');

print "Target soundex of $ARGV[0] is $targetSoundex\n";

while(<FH>) {
    print "Soundex of $_ is ".soundex($_);
    if($targetSoundex eq soundex($_)) {
        print " (match).\n";
    }else {
        print " (no match).\n";

Make it executable and run some examples:

$ chmod +x soundslike.pl 
$ ./soundslike.pl michael
Target soundex of michael is M240
Soundex of jacob is J210 (no match).
Soundex of Jakob is J210 (no match).
Soundex of miguel is M240 (match).
Soundex of Michael is M240 (match).
$ ./soundslike.pl jagub
Target soundex of jagub is J210
Soundex of jacob is J210 (match).
Soundex of Jakob is J210 (match).
Soundex of miguel is M240 (no match).
Soundex of Michael is M240 (no match).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .