AI Hints
Home›Linux Tutorial›Patterns and Practice
Linux · Lesson 58 of 120

Simple Patterns

^ $ and . in grep.

⏱ 2 min read▶ Video at 1:14:57🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

So far, grep has searched for plain words. But grep understands patterns too, called regular expressions. You don't need to learn them all. Just a few symbols make your searches much smarter.

Simple Patterns
From the video: Simple Patterns
  • grep understands patterns
  • Regular expressions
  • A few symbols go far

Start and end of a line

A caret means the start of a line. Caret C finds words that start with C. A dollar sign means the end of a line. T dollar finds words that end in T. Without these, grep C would match scatter too, because it contains a C.

Terminal
grep '^c' words.txt
cat
cart
coat
cot
grep 't$' words.txt
cat
cart
coat
cot
act

Any single character

A dot matches any one character. C dot T matches cat, cot, and the CAT inside scatter. Combine it with the caret and the dollar, and caret C dot T dollar matches only three letter words, starting with C and ending with T.

Terminal
grep 'c.t' words.txt
cat
scatter
cot
grep '^c.t$' words.txt
cat
cot

A tiny pattern cheat sheet

Here are the symbols worth remembering. Caret for the start, dollar for the end, dot for any character. Star means repeat the thing before it, any number of times, so dot star means anything at all. And square brackets match one character from a set. Always put patterns in single quotes, so the shell leaves them alone.

SymbolMatches
^start of the line
$end of the line
.any one character
.*anything at all
[abc]one of a, b or c

Summary

Caret is the start, dollar is the end, and dot is any character. Quote your patterns. Next, let's use everything to write a log report.

Key takeaways
  • ^ start, $ end
  • . any character
  • Single quotes
Next up: Practice: A Log Report — pipelines in action