Home > Net >  How to find all non-dictionary words in a file in bash/zsh?
How to find all non-dictionary words in a file in bash/zsh?

Time:07-10

I'm trying to find all words in a file that don't exist in the dictionary. If I look for a single word the following works

b=ther; look $b | grep -i "^$b$" | ifne -n echo $b => ther
b=there; look $b | grep -i "^$b$" | ifne -n echo $b => [no output]

However if I try to run a "while read" loop

while read a; do look $a | grep -i "^$a$" | ifne -n echo "$a"; done < <(tr -s '[[:punct:][:space:]]' '\n' <lotr.txt |tr '[:upper:]' '[:lower:]')

The output seems to contain all (?) words in the file. Why doesn't this loop only output non-dictionary words?

CodePudding user response:

Regarding ifne

If stdin is non-empty, ifne -n reprints stdin to stdout. From the manpage:

   -n   Reverse operation. Run the command if the standard input is empty

        Note that if the standard input is not empty, it is passed through
        ifne in this case.

strace on ifne confirms this behavior.

Alternative

Perhaps, as an alternative:

  1 #!/bin/bash -e
  2 
  3 export PATH=/bin:/sbin:/usr/bin:/usr/sbin
  4 
  5 while read a; do
  6     look "$a" | grep -qi "^$a$" || echo "$a"
  7 done < <(
  8     tr -s '[[:punct:][:space:]]' '\n' < lotr.txt \
  9     | tr '[A-Z]' '[a-z]' \
 10     | sort -u \
 11     | grep .
 12 )
  • Related