Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How to find and print out English words contained in a file via linux command line?

share|improve this question

4 Answers 4

GNU grep has the following options:

grep --only-matching --ignore-case --fixed-strings --file /usr/share/dict/british-english-insane /path/to/file.txt

This outputs strings found one-per-line. Here /usr/share/dict/british-english-insane is a wordlist provided by the Debian package wbritish-insane.

share|improve this answer

He, funny !

file=/usr/share/licenses/common/GPL3/license.txt
dict=/usr/share/dict/cracklib-small

while read word; do
    grep >/dev/null -i "\<$word\>" $file &&
        printf 'Word "%s" found in GPLv3...\n' $word
done < $dict

Output :

Word a found in GPLv3...
Word ability found in GPLv3...
Word about found in GPLv3...
(...)

The cracklib-small file comes with the package cracklib http://sourceforge.net/projects/cracklib

share|improve this answer
    
Corrected a mistaken input file –  sputnick Oct 2 '12 at 1:48
    
If you use -q instead of >/dev/null, it will imply -m1 and it will not read the entire file every iteration of the loop. Personally, I would store the file in memory if it was small enough. –  jordanm Oct 2 '12 at 5:02
1  
This is extremely (big-time!) slow... cracklib-small has only 52,875 words and it has been running for over half an hour (compare to my and donothingsuccessfully's times, see my answer. That dictionary has 390,000 words) ... you are calling grep too many times.. try for once. –  Peter.O Oct 2 '12 at 12:36
    
My first thought was not to make something robust & quick but making a simple script that work's and simple to understand for beginners. –  sputnick Oct 3 '12 at 11:06

grep based solutions will generally be quite slow especially with large word lists.

You can take advantage from the fact that word lists are already sorted (however on my system, it seems at least the british-english one has been sorted in the POSIX/C locale even though it's UTF-8 encoded):

tr -cs "[:alpha:]'" '[\n*]' < /etc/passwd |
  LC_ALL=C sort -u |
  LC_ALL=C comm -12 - /usr/share/dict/british-english-insane

You may also want to convert everything lowercase or uppercase beforehand if you want to look for words in a case-insensistive manner.

share|improve this answer
    
This is good; tr -c is much simpler than excluding values manually... Perhaps it could use some case (insensitive) awareness, as the dictionary is lower-case or titl-case only. +1 –  Peter.O Oct 2 '12 at 13:41
file=/usr/lib/python2.6/LICENSE.txt
dict=/usr/share/dict/british-english-huge   # or any suitable list

sort "$dict" \
     <(sed "s/[].,\"?!;:#$%&()*+<>=@\^_{}|~[]\+/\n/g   # keep ' for now
            s|[-/[[:digit:][:blank:][:cntrl:]]\+|\n|g
            s/\<'\+/\n/; s/'\>\+/\n/                   # remove '
           " <(<"$file" tr '[:upper:]' '[:lower:]') ) |
uniq -c | awk '$1 > +1 {print $2}' 

found 382 words (case insensitive) in time:

real   0m1.723s
user   0m1.872s
sys    0m0.048s
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.