Spellcheck each line
I'd like to write a bash filter that will take a file of n开发者_StackOverflowewline-separated sentences and return sentences that are not misspelled. I've been thinking about aspell but I'm not sure what to do with it. Any ideas?
This pipe should give the results you want. Note that you should pipe something into this, so prepend e.g. cat input.txt |
for a quick test.
while read line; do [ "$(ispell -l <<< "$line" | wc -l)" -gt 0 ] && echo "$line"; done
To also prepend a line number:
nl -b a -p | while read number line; do [ "$(ispell -l <<< "$line" | wc -l)" -gt 0 ] && echo "$number: $line"; done
If you want to return misspelled lines instead, just replace -gt
by -le
(or replace &&
by ||
, of course)
Of course you can save these lines as a script, and then simply do
script.sh < input.txt
if you so prefer
Here is a script which does what you want.
#!/bin/bash
# Regex for lines describing "good words":
# - empty lines (after each line of input, i.e. at the end)
# - lines with only a '*' (indicating a good word)
# - a line with '@(#) ' (at the start of the output)
# All other lines indicate a bad word.
good_words='^[*]?$|^@\(#\) '
while read # read one line of input
do
echo $REPLY | # pipe the line to aspell
aspell pipe | # let aspell check the line
egrep -q -v $good_words || # have a look if aspell found misspellings
# no words with mistake, output the line
echo $REPLY
done
grep -v "$(aspell list < file)" file
精彩评论