Skip to content

Both.org

News, Opinion, Tutorials, and Community for Linux Users and SysAdmins

Primary Menu
  • About Us
  • Computers 101
    • Hardware 101
    • Operating Systems 101
  • End of 10 Events
    • Wake Forest, NC, — 2025-09-20
  • Linux
    • Why I use Linux
    • The real reason we use Linux
  • My Linux Books
    • systemd for Linux SysAdmins
    • Using and Administering Linux – Zero to SysAdmin: 2nd Edition
    • The Linux Philosophy for SysAdmins
    • Linux for Small Business Owners
    • Errata
      • Errata for The Linux Philosophy for SysAdmins
      • Errata for Using and Administering Linux — 1st Edition
      • Errata for Using and Administering Linux — 2nd Edition
  • Open Source Resources
    • What is Open Source?
    • What is Linux?
    • What is Open Source Software?
    • The Open Source Way
  • Write for us
    • Submission and Style guide
    • Advertising statement
  • Downloads
  • Home
  • Using ‘grep’ to play a word game
  • Command Line
  • Fun
  • Linux

Using ‘grep’ to play a word game

You can use regular expressions to match letters and patterns, to help you play a letter game.
Jim Hall May 13, 2024 6 minutes read
The Bash logo

The Bash logo

Sometimes I need to take a break from what I’m doing and let my mind relax. And a fun way to do that is to play a simple puzzle game. You might be familiar with Wordle, the word puzzle game where you make successive attempts to guess a secret five-letter word that changes every day. For each guess, the game tells you which letters are correct and in the correct location (green), which letters are correct but in the wrong position (yellow), and which letters don’t actually appear in the secret word (gray).

I find that this can be a relaxing game to play when I need a quick break. And when I play the game, I like to use the grep command to exercise regular expressions. Using grep isn’t really cheating, it’s just a way to help narrow down my options.

Start with a list of words

To get started, you’ll need to have a list of five-letter words. Linux provides this in the /usr/share/dict/words file, but this file contains all kinds of words, including names and other proper nouns (like Linus), some number-based words (such as 12-point and 1st), and acronyms (like SPARC). Wordle doesn’t allow these kinds of words, it only uses all-lowercase words. To get a list of all-lowercase five-letter words, we can use the character pattern [a-z] which matches a single lowercase letter from a to z. If we use this multiple times, and combine it with ^ to match the start of a line, and $ for the end of a line, we’ll have a list of words that are all-lowercase and exactly five letters long:

$ grep '^[a-z][a-z][a-z][a-z][a-z]$' /usr/share/dict/words > wordlist

This looks for words in /usr/share/dict/words that are composed of exactly five lowercase letters, and saves the output in a new file called wordlist in the current directory. On my system, that list is over 15,000 words long!

$ wc -l wordlist
15034 wordlist

Narrow down the options

Start the game by guessing a word that has five letters. To help narrow down the options, I like to pick a word that has five unique letters, rather than a word with repeated letters, like boots. Some of the most commonly used letters in English include E, S, T, and R, so I’ll start by guessing the word stare.

Let’s use grep to help narrow down my possible next guesses. The gray and yellow letter tiles tell me that today’s secret word doesn’t contain the letters S, T, or A. The secret word does contain R and E, but not as the last two letters.

First, let’s narrow down the options to eliminate words that do not contain S, T, or A. The -v option for grep is very handy here to “invert” a search. For example, if we “invert” the search for any words with S, T, or, A, grep will return only the words that do not contain those letters. This already reduces our options from 15,000 possible words in the first guess to only 3,600 possible words for our second guess:

$ grep -v '[sta]' wordlist > guess2a
$ wc -l guess2a 
3640 guess2a

But this list also includes words like chide, which has the letter E in the last position, or the word berry which has an R in the next-to-last position. Wordle colored those letter tiles yellow after our first guess, to indicate that the secret word had both R and E in it, but not in those positions. So to narrow down our possible list of guesses, we need to eliminate any words with an E as the last letter, or an R as the second-to-last letter. This brings the list down to only 550 possible words:

$ grep e guess2a | grep -v 'e$' | grep r | grep -v 'r.$' > guess2b
$ wc -l guess2b
553 guess2b

The period in r.$ is a placeholder for any possible character. In this case, since our list only contains words with five letters, this regular expression effectively means “the letter R as the next-to-last letter.”

Make another guess

As I look through my list of words to make my next guess, I want to pick an “everyday” word that has five unique letters. For example, the word creek is good, but it has two E’s. Instead, I decided to guess the word biker.

Guessing a word that has five unique letters provides me additional information about what letters might appear in the word. For example, the gray and yellow tiles tell me that the secret word does not contain the letters B or I. It does have a K in it, but not as the middle letter.

We can use grep again to further narrow down the options. As before, the first step is to eliminate any words that have B or I. This narrows the list to just over 300 possible words:

$ grep -v '[bi]' guess2b > guess3a
$ wc -l guess3a
325 guess3a

Then, filter the list to only find words with K, E, and R, but not as the last three letters. Since we already filtered the word list to only contain R and E words, we don’t need to run grep with those letters, but we need to grep for any words with K:

$ grep k guess3a | grep -v '^..k' | grep -v 'e.$' | grep -v 'r$' > guess3b
$ wc -l guess3b
9 guess3b

This brings the list down to only nine possible words:

$ cat guess3b
dreck
freck
jerky
kerch
kreng
perky
reeky
renky
wreck

Guess the word

From here, guessing the secret word within six total attempts should be pretty easy. Wordle tends to use “everyday” words, so we can pick a word like wreck for the next guess.

This is getting close! We now know the word doesn’t contain W or C, and the letters R, E, and K are in the wrong positions:

$ grep -v w guess3b | grep -v c | grep -v '^.r' | grep -v '^..e' | grep -v 'k$' > guess4a
$ wc -l guess4a
3 guess4a

This narrows down the list of possible words to just three:

$ cat guess4a
jerky
perky
renky

I’ll guess the word jerky, which happens to be correct!

Regular expressions for the win

The grep command is a powerful tool that lets you find words in a list based on regular expressions. This example shows how to use grep to help narrow down the options in a word puzzle game, but you can use grep in the same way to match other things. For example, system administrators might use grep to find errors in a log file, such as the /var/log/messages file, but only for a particular day. With grep, you can match text at the beginning of a line, the end of a line, or anywhere in between – or find lines that do not contain the text pattern. Add grep to your systems administrator “toolkit” to make your work easier.

Tags: command line grep Linux Regular Expressions

Post navigation

Previous: Why it’s important for leaders to mentor and support others
Next: How to build rpm packages

Related Stories

connections_wires_sysadmin_cable
  • Linux
  • Networking
  • Router

How to Make your Linux Box Into a Router

David Both April 29, 2026
f44-01-day-cropped
  • Fedora
  • Linux
  • Upgrades

Fedora 44 Released

David Both April 28, 2026
command_line_prompt
  • Command Line
  • Linux
  • Programming

Writing a replacement seq command

Jim Hall April 27, 2026

System upgrades this Sunday, May 3

Tools illustrationFedora 44 was released this week and I’ve upgraded all my systems except for the two that directly affect Both.org. I’ll be upgrading the hosts for my server and firewall to Fedora 44 this Sunday afternoon, May 3.

Both.org will be down for most of the afternoon for these upgrades.

Thanks for your patience.

Random Quote

There’s always one more bug.

— Lubarsky’s law of cybernetic entomology

Why I’ve Never Used Windows

On February 12 I gave a presentation at the Triangle Linux Users Group (TriLUG) about why I use Linux and why I’ve never used Windows.

Here’s the link to the video: https://www.youtube.com/live/uCK_haOXPFM 

Why there’s no such thing as AI

Last October at All Things Open (ATO) I was interviewed by Jason Hibbits of We Love Open Source. It’s posted in the article “Why today’s AI isn’t intelligent (yet)“.

Technically We Write — Our Partner Site

Our partner site, Technically We Write, has published a number of articles from several contributors to Both.org. Check them out.

Technically We Write is a community of technical writers, technical editors, copyeditors, web content writers, and all other roles in technical communication.

Subscribe to Both.org

To comment on articles, you must have an account.

Send your desired user ID, first and last name, and an email address for login (this must be the same email address used to register) to subscribe@both.org with “Subscribe” as the subject line.

You’ll receive a confirmation of your subscription with your initial password as soon as we are able to process it.

Administration

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

License and AI Statements

Both.org aims to publish everything under a Creative Commons Attribution ShareAlike license. Some items may be published under a different license. You are responsible to verify permissions before reusing content from this website.

The opinions expressed are those of the individual authors, not Both.org.

You may not use this content to train AI.

 

Advertising Statement

Both.org does not sell advertising on this website.


Advertising may keep most websites running—but at Both.org, we’re committed to keeping our corner of the web ad-free. Both.org does not sell advertising on the website. Nor do we offer sponsored articles at this time. We’ll update this page if our position on sponsorships changes.

We want to be open about how the website is funded. Both.org is supported entirely by David Both and a few other dedicated individuals.

 

 

Copyright © All rights reserved. | MoreNews by AF themes.