Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to use GNU’s aspell to fix spelling errors in files

How-To
Aug 07, 20174 mins
Linux

The GNU aspell tool is a very clever utility for not only checking your spelling, but also fixing it

GNU’s aspell is a very useful tool for fixing potential typos in files. It not only picks out your misspellings and displays them to you, but it offers you a list of potential corrections and applies your changes as instructed. And it often remembers the fixes that you’ve applied.

Hopefully, you’ve spotted the typo in this post’s image. If you had a file containing the word “appertizers,” this clever utility would help you to spot and replace it.

Say you had a file named “oops” that contained this typo:

$ cat oops
Please list the appertizers in alphabeticle order.

If you asked aspell to check this file with the command “apsell check oops”, it would present the file contents with the word “appertizer” highlighted and offer the list below as options for correcting the error.

1) appetizers                           6) appetite's
2) appetizer's                          7) appraisers
3) appetizer                            8) baptizers
4) appetites                            9) amortizes
5) apertures                            0) appertains
i) Ignore                               I) Ignore all
r) Replace                              R) Replace all
a) Add                                  l) Add Lower
b) Abort                                x) Exit

Options 1-0 are replacement words. If you then pressed the “1” key, aspell would make the selected correction in the file from the list of suggestions. As you can see, you’d have nine other replacement spelling/word options to choose from in case “appetizers” isn’t exactly what you were trying to write.

You could also choose to ignore this misspelling and move on to the next (i), ignore all misspellings (I), replace the word with something altogether—something you would be prompted to enter (r), or replace all instances of the misspelling in the file with something you’d be prompted to enter (R).

The Add (a) and Add lower (l) options will add the detected word to your personal dictionary—a list of words that aspell will consider to be correct. This is useful if you don’t want the spelling of a product or some code word to be highlighted as a mistake every time it’s encountered.

Once you made a decision on how to handle “appertizers”, aspell would move on to the next misspelling in the file—the word “alphabeticle”. Once that error is fixed as well, aspell will simply exit, having made all the needed corrections in your file.

If you get no response from aspell, that means it didn’t find anything to object to in your file. Take this as a clean bill of health—no typos. Of course, it’s not going to find grammatical errors. In general, spell checkers don’t do that.

$ cat dumfile
I is a happy camper go and see they is two.
$ aspell check dumfile
$             

The aspell tool will also save a backup of your original file—in this case, oops.bak—unless you tell it not to by using the –dont-backup option.

One way to make your use of aspell a little easier is to save your preferences using an alias like this:

alias fix='aspell check --dont-backup'

Checking individual words

You can also get aspell to help you with the spelling of some particular word. If you were not sure, for example, how to spell the word "alphabetical", you could use the tool to come up with suggestions for the word you're looking for:

$ echo alphabeticle | aspell pipe
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.7-20110707)
& alphabeticle 4 0: alphabetical, alphabetic, alphabetically, alphabetize

Notice that aspell offered four options for the word you were likely looking for and that the most likely word is first in the list.

Why aspell?

The key benefit of aspell is that it seems to make better replacement suggestions than other spell checkers. It can also remember word replacements that you use to facilitate their use in the future—unless you tell it not to do so with the –dont-save-repl option (shown in the alias below).

alias fix='aspell check --dont-backup --dont-save-repl'

You might not want to save replacement words if they're very specific to the file you're correcting.

The aspell utility "remembers" the choices you make by creating a couple files in your home directory. One is called .aspell.lang.prepl and the other .aspell.lang.pws where lang is replaced by a language designator. In my case, this files are .aspell.en.prepl and .aspell.en.pws.

The .pws file is a personal dictionary. The .prepl file is a personal replacement dictionary.

The aspell command is a very sophisticated spell checker that can help correct your errors, though it isn't going to help if you can't keep words like "there" and "their" straight.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author