Skip to content
Intermediate LinuxLesson 21 of 27 3 min read

Regular Expressions

Anchors, character classes, quantifiers, and alternation - pattern matching with grep and sed.

On this page8 sections

grep's -i/-v/-n flags and sed's s/find/replace/ only get you so far when "find" needs to mean "any of these shapes" instead of one exact string. A regular expression (regex) is a tiny pattern language for describing shapes of text - and it's the single most useful skill layered on top of everything you've learned about grep and sed so far.

The practice terminal's grep and sed use real, JavaScript-flavored regex syntax (the same family as grep -E, Python, and Perl) - bare parentheses for grouping, no backslash needed before +, ?, or |. That is the modern, most-transferable dialect, so every pattern in this lesson runs there exactly as written.

Anchors: where in the line

bash
grep '^ERROR' log.txt      # lines that START WITH "ERROR"
grep 'timeout$' log.txt    # lines that END WITH "timeout"
grep '^$' log.txt          # completely empty lines

^ anchors to the start of the line, $ anchors to the end. Without an anchor, a pattern matches anywhere in the line.

Character classes: one of a set

bash
grep '[0-9]' log.txt       # any line containing at least one digit
grep '[A-Z]' log.txt       # any line containing an uppercase letter
grep '[^0-9]' log.txt      # lines containing a NON-digit character

[...] matches exactly one character from the set inside the brackets. A leading ^ inside the brackets inverts the set - [^0-9] means "any character that isn't a digit."

The dot: any character

bash
grep 'l.ne' log.txt        # matches "line", "lane", "l3ne" - any single character in that spot

. matches literally any one character. If you actually want to match a literal period, escape it: \..

Quantifiers: how many

SymbolMeans
*zero or more of the previous thing
+one or more
?zero or one (optional)
{2,4}between 2 and 4, inclusive
bash
grep 'ERRO*R' log.txt      # "ERR", "ERROR", "ERROOOR" - zero or more O's
grep '[0-9]+' log.txt      # one or more digits in a row
grep 'colou?r' log.txt     # matches BOTH "color" and "colour"

Alternation: this OR that

bash
grep -E '^(ERROR|WARN)' log.txt   # lines starting with either word

| means "or," and parentheses group the alternatives so ^ applies to the whole (ERROR|WARN), not just ERROR. -E isn't strictly required in the practice terminal (its regex is always "extended"), but typing it is good muscle memory for real grep, where plain grep needs it to enable |, +, ?, and unescaped ().

Groups and backreferences with sed

Parentheses don't just group alternatives - they capture the text they matched, so you can reuse it in a replacement. sed refers to captured groups as \1, \2, and so on:

bash
echo "John Smith" > name.txt
sed 's/([A-Z][a-z]+) ([A-Z][a-z]+)/\2 \1/' name.txt

That swaps first and last name: group 1 captures "John," group 2 captures "Smith," and the replacement \2 \1 writes them back in reverse order.

Try it

Run these in the practice terminal (opens in a new tab). It opens in a new tab, so you keep your place here.
bash
printf 'ERROR: disk full\nWARN: low memory\nline three\n' > log.txt
grep '^ERROR' log.txt
grep -E '^(ERROR|WARN)' log.txt
grep '[0-9]' log.txt
echo "John Smith" > name.txt
sed 's/([A-Z][a-z]+) ([A-Z][a-z]+)/\2 \1/' name.txt

Further reading

  • "Mastering Regular Expressions" by Jeffrey Friedl (O'Reilly) - the definitive, exhaustive book on regex across every major tool and language.
  • "sed & awk" by Dale Dougherty and Arnold Robbins (O'Reilly) - ties regex directly into real sed and awk usage, with the classic POSIX dialects this lesson's modern syntax evolved from.

Challenge

In log.txt, print every line that starts with either ERROR or WARN, using a single grep command with -E.

Want to experiment first? Try things in the practice terminal (opens in a new tab), then enter the command that solves the task here.

Quiz

Lock in the concepts. Score at least 80% to mark the lesson complete.

0 of 3 answered

Done with this one?

Skipped the challenge? You can mark the lesson complete by hand.

Your notes

Kept on this browser only.