Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GREP How do I search for words that contain specific letters (one or more times)?

Tags:

grep

I'm using the operating systems dictionary file to scan. I'm creating a java program to allow a user to enter any concoction of letters to find words that contain those letters. How would I do this using grep commands?

like image 599
stackoverflow Avatar asked Oct 16 '25 16:10

stackoverflow


1 Answers

To find words that contain only the given letters:

grep -v '[^aeiou]' wordlist

The above filters out the lines in wordlist that don't contain any characters except for those listed. It's sort of using a double negative to get what you want. Another way to do this would be:

grep '^[aeiou]+$' wordlist

which searches the whole line for a sequence of one or more of the selected letters.

To find words that contain all of the given letters is a bit more lengthy, because there may be other letters in between the ones we want:

cat wordlist | grep a | grep e | grep i | grep o | grep u

(Yes, there is a useless use of cat above, but the symmetry is better this way.)

like image 192
Greg Hewgill Avatar answered Oct 19 '25 10:10

Greg Hewgill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!