Read

Module 4 · Finding things

Regular expressions, part 1: ., *, ^, $

The four characters that turn a grep pattern from a fixed string into a description: any character, repetition, start of line and end of line.

What you will learn

  • Explain what ., *, ^ and $ mean inside a pattern.
  • Anchor a pattern to the start or end of a line and find empty lines.
  • Escape a metacharacter to match it literally, or use grep -F.

So far grep patterns were plain words. A regular expression (regex) is a pattern in a small language where a few characters have special meaning. With just four of them you can say "lines that start with a hash", "lines that end in a digit", "empty lines" or "the word color, however it is spelled". The same syntax works in grep, sed, awk, vi and most programming languages, so what you learn here travels well.

The four characters

CharMeaningExample
.any single character (except newline)c.t matches cat, cot, cut, c9t
*zero or more of the preceding itemab*c matches ac, abc, abbbc
^start of line^# lines beginning with #
$end of line;$ lines ending in ;

* is the one that trips people up, because it does not mean what it means in file names. In a shell wildcard * is "anything"; in a regex it is "repeat the previous thing zero or more times". So .* is "anything" (any character, repeated), and a* matches the empty string too, which is why grep 'a*' file prints every line. The very common .* lets you say "this, then anything, then that": grep 'start.*end'.

Anchors

Without anchors a pattern matches anywhere in the line. ^ pins it to the start, $ to the end, and both together make it match the whole line: grep '^root

TempMV — Temporary Virtual Machines in your browser

TempMV creates, boots and throws away real temporary x86 virtual machines directly in your browser — nothing to install, no account needed. It uses the v86 hypervisor (WebAssembly) to run real machine code. Reload the page and the machines power off and disappear: they are throwaway / disposable by design.

También en español: TempMV crea y desecha máquinas virtuales temporales y efímeras en el navegador, sin instalar nada y sin necesidad de cuenta.

TempMV needs JavaScript and WebAssembly enabled to run.

finds lines that are exactly root. The pattern ^$ has nothing between start and end, so it matches empty lines; grep -c '^ file counts them and grep -v '^ file removes them. Anchors only mean start and end when they are at the start and end of the pattern; a^b would look for a literal caret.

~% cd /root/lab/l33
l33% cat words.txt
undo
cat
running
unit
cut
sing
cot

act
l33% grep '^un' words.txt
undo
unit
l33% grep 'ing
  

 words.txt
running
sing
l33% grep 'c.t' words.txt
cat
cut
cot
l33% grep -c '^
  

 words.txt
1
l33% grep 'u.*t' words.txt
unit
cut

Matching the special characters themselves

To search for a literal dot, star, caret or dollar, put a backslash in front: grep '\.txt

TempMV — Temporary Virtual Machines in your browser

TempMV creates, boots and throws away real temporary x86 virtual machines directly in your browser — nothing to install, no account needed. It uses the v86 hypervisor (WebAssembly) to run real machine code. Reload the page and the machines power off and disappear: they are throwaway / disposable by design.

También en español: TempMV crea y desecha máquinas virtuales temporales y efímeras en el navegador, sin instalar nada y sin necesidad de cuenta.

TempMV needs JavaScript and WebAssembly enabled to run.

finds lines ending in .txt, whereas grep '.txt would also match atxt. Alternatively, grep -F (or fgrep) treats the whole pattern as fixed text with no special characters at all, which is the right choice when you are searching for something like an IP address or a price with a dot in it.

These four characters are the core of the *basic* regular expression syntax that grep and sed use by default. The next lesson adds character classes, alternatives and groups, which need the extended syntax of grep -E.

Commands in this lesson

CommandWhat it does
grep '^word' fileLines starting with word.
grep 'word fileLines ending with word.
grep '^word fileLines that are exactly word.
grep -v '^ fileRemove empty lines.
grep 'c.t' filec, any one character, t.
grep 'a.*b' filea, then anything, then b.
grep '\.txt fileA literal dot: escape it.
grep -F '1.2.3.4' fileFixed string, nothing is special.

Quiz

  1. What does `.` match in a regular expression?

    • A literal dot only
    • Any single character
    • Any number of characters
  2. Which pattern matches lines that end in a semicolon?

    • `^;`
    • `; TempMV (TempVM) — Temporary Virtual Machines in your browser · Máquinas virtuales temporales
    • `;*`
  3. What does `grep 'ab*c'` match?

    • Only `abc`
    • `ac`, `abc`, `abbc`... (zero or more b)
    • `ab` followed by anything then `c`
  4. How do you count the empty lines in a file?

    • `grep -c '^ file`
    • `grep -c ' ' file`
    • `grep -c '.*' file`
  5. Which command finds lines that end in `.log`, with a real dot?

    • `grep '.log file`
    • `grep '\.log file`
    • `grep '*.log' file`

Practice

  1. /root/lab/l33/words.txt is a word list. Save the lines that START with `un` into /root/lab/l33/un.txt (a word merely containing `un` elsewhere must not be included).

  2. Save the lines of /root/lab/l33/words.txt that END with `ing` into /root/lab/l33/ing.txt.

  3. Save into /root/lab/l33/cxt.txt the lines of /root/lab/l33/words.txt that contain a `c`, then exactly one character of any kind, then a `t` (so `cat`, `cut` and `cot`, but not `act`).

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.