Terminal is an Apple utility that gives users control over their Mac via a command line, enabling them to use Unix commands to accomplish all sorts of tasks. For instance, you might be surprised to learn that you can play games like Tetris and Pong from within the terminal app. But today we’re going to look at something a little bit more nerdy than that – words.
Grep is a particular Unix command dating back to 1973 that searches globally for lines matching a regular expression, and prints them – Globally/RegularExpression/Print.
In the simplest of examples, if you type grep johnson NBA_Players.txt into the command line, terminal will list out all entries from the file NBA_Players.txt that contain the word Johnson (Note: there’s no built-in file filled with NBA players on your Mac!)
But once you start using regular logical expressions, the Grep command becomes much more powerful and makes for a great way to cheat while playing Scrabble or Words With Friends on the iPhone.
Once you open up Terminal, type in the following command: cd /usr/share/dict
This changes the directory and opens up your Macs built-in list of words it uses for the Dictionary app.
Now let’s assume that your Scrabble letters are ‘laherbt’ – To figure out all possible words you can derive from that list, type the following command
grep -x ‘[laherbt]*’ web2
The result is a long list of words containing all those letters. Note, however, that the grep command will re-use letters so be careful to scan the list for words that don’t do that. As a point of interest, the actual file being searched here is web2, which is in reference to Webster’s Second International Dictionary and contains 234,936 words from the 1934 edition.
Now let’s say you’re looking for a list of words that starts with a particular sequence. For this example, we’ll use our letters ‘laherbt’ again. Assume you want a list of words that incorporate the letters you have but that starts with the sequence, “ar”.
Simply type grep -x ‘^ar[laherb]*’ web2 into the command line and you’re all set. Note the difference here is the presence of the carrot (^) and the letters desired at the beginning of the word (ar) before the left bracket.
Conversely, if you’e like a list of words that end with a particular sequence of letters, simply type those letters after the asterik, and follow that with a $.
For example, to find a list of words that end in ‘er’ from the sequence ”laherbt”, type the following command into the terminal app.
grep -x ‘[laherbt]*er$’ web2
You can also combine both of the aforementioned commands to find words that start with sequence of letters and end with a specified sequence of letters.
So, if we want to find a list of words that start with ‘t’ and end with ‘er’, the following command will yield that result.
grep -x ‘^t[laherbt]*er$’ web2
And with that you should be good to go on your path towards Scrabble and Words With Friends domination.
And one more thing, if you really wanna nerd it up, you can have Terminal output the list of words in color by adding a –color instruction after the grep command like so.
grep –color -x ‘^t[laherbt]*el$’ web2