03. Matching single characters

Using literal characters

Let's start by matching a single character. The most obvious way would be to use a literal character for alphabetic letters. If there is a metacharacter that you would like to match, you can escape it with the backslash (\).

$ ls /usr/bin | grep 'c\.d'
creatbyproc.d filebyproc.d newproc.d pidpersec.d runocc.d syscallbyproc.d syscallbysysc.d

Here's a simple regex example. We are looking for patterns that has a c, followed by a period (.), then a d.

From this we can notice two things:

  • We need to use the backslash to escape metacharacters (the period).
  • Order matters.
  • This isn't a "fuzzy search", where cat can match something like combat.

Using dot metacharacter

The dot (.) metacharacter matches any single character.

$ ls /usr/bin/ | grep 'c.t'
bzcat codesign_allocate colcrt cut gencat gzcat locate policytool tccutil xmlcatalog ypcat zcat

Notice how a character must be present within between c and t. The dot metacharacter does not allow for the NUL character.

In the next section, we'll learn how to use bracket expansions, which will give you even more power in selecting single characters.

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Ad