Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 mach 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?

share|improve this question
3  
agrep for approximate grep (not for sound/language). Also in zsh pattern matching (#a3) for allow up to 3 mistakes. –  Stéphane Chazelas Jun 14 '13 at 9:36
6  
Take a look at the Text::Soundex perl core module too: pastebin.com/UbeVFBQA –  manatwork Jun 14 '13 at 10:40
1  
@manatwork - you should write that up as an answer! –  slm Jun 14 '13 at 14:01
1  
@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 '13 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

jacob
Jakob
miguel
Michael

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

#!/usr/bin/perl

use Text::Soundex;

open(FH, 'names.txt');

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

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

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).
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.