Spell checking a file using command line, non-interactively

和自甴很熟 提交于 2019-12-04 01:54:05

You can experiment with commands like these:

yes 0 | script -c 'ispell text.txt' /dev/null

or:

yes 1 | script -c 'aspell check text.txt' /dev/null

But keep in mind that the results can be poor even for simple things:

$ echo The quik broown fox jmps over the laazy dogg > text.txt
$ yes 0 | script -c 'ispell text.txt' /dev/null
Script started, file is /dev/null
Script done, file is /dev/null
$ cat text.txt
The quick brown fox amps over the lazy dog

It seems to be even worse with aspell so probably it's better to go with ispell.

You need the script command because some commands like ispell doesn't want to be scripted. Normally you would pipe the output of yes 0 to a command to simulate hitting the "0" key all the time but some commands detect being scripted and refuse to cooperate:

$ yes 0 | ispell text.txt
Can't deal with non-interactive use yet.

Fortunately they can be fooled with the script command:

$ yes 0 | script -c 'ispell text.txt' /dev/null
Script started, file is /dev/null
Script done, file is /dev/null

You can use other file than /dev/null to log the output:

$ yes 0 | script -c 'ispell text.txt' out.txt
Script started, file is out.txt
Script done, file is out.txt
$ cat out.txt 
Script started on Tue 02 Feb 2016 09:58:09 PM CET

Script done on Tue 02 Feb 2016 09:58:09 PM CET

If you don't need it to replace every wrong word, but simply point out the errors and print suggestions in a non-interactive manner, you can use ispell:

$ ispell -a < file.txt | grep ^\& > errors.txt

I'm unfortunately not aware of any standard Linux utility that does what you're requesting from the command line, although the emacs suggestion in the comments above comes close.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!