Spell Checking a File Using Command Line, Non-Interactively

Spell Check for Properties file

On unix-like systems, you can use a combination of tools on the command line. One example would be to use egrep to filter out lines starting with comment characters, cut to split the properties file on the equals sign and output the second field (right-hand side), and a pipe to a spell checker like spell.

Given this input file named resources.properties:

# Application resources
com.hello.world = Hello world.
com.module.home = hoem.
com.module.fruits = apple, bananananana, cranberry, grape, orange, strawburry

Here's an example of a command to spell check "resources.properties", and its output:

[user@host ~]$ egrep -v '^#' resources.properties | cut -d= -f2 | spell
bananananana
hoem
strawburry

The egrep -v '^#' command filters out comments (lines that start with the pound sign). The -d= argument to the cut utility sets the field delimiter to the equals sign, and -f2 directs the command to output the second field -- the right-hand side of the equals sign, a.k.a. the property value / string to spell-check.

On many systems, the "spell" command is provided by the aspell utility. An alternate command line that invokes aspell directly, and offers suggestions for spelling correction:

egrep -v '^#' resources.properties | cut -d= -f2 | aspell pipe

How to run spell check on multiple files and display any incorrect words in shell script?

You can install aspell with Homebrew on OS X. brew info aspell lists supported languages.

brew install aspell --lang=en,fi,jp

aspell check opens a file in an interactive spell checker:

for f in *.txt; do aspell check $f; done

aspell list prints all unrecognized words:

cat *.txt | aspell list | sort -u

Learned words are stored in .aspell.en.pws by default. You can also exclude words in ~/Library/Spelling/en after adding personal_ws-1.1 en as the first line.

aspell list --personal=$HOME/Library/Spelling/en

Bash - omit lines starting with a mis-spelled word (using hunspell)

The easiest way would be to use the join command:

$ join words.txt ok_words.txt 
bird 10
cat 12
dog 42
fish 2

or to preserve tabs:

$ join -t $'\t' words.txt ok_words.txt 
bird 10
cat 12
dog 42
fish 2

Passing arguments to an interactive program non-interactively

For more complex tasks there is expect ( http://en.wikipedia.org/wiki/Expect ).
It basically simulates a user, you can code a script how to react to specific program outputs and related stuff.

This also works in cases like ssh that prohibits piping passwords to it.



Related Topics



Leave a reply



Submit