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.

Take your Linux skills to the next level!

Linux for Beginners

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

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad