Skip to content

You search for error and get both a complete status word and a filename containing that word. The search found the letters you requested, but your question was narrower: which line contains exactly that status?

By the end, you can express that boundary, select a digit, repeat a character, and match a literal dot. You can also explain why copying a shell wildcard into grep changes its meaning.

Finish Searching inside files (B10.2). Keep your B04 lab directory. Prepare these fixed sample lines and empty files only in ~/lab/b10/regex:

Terminal window
mkdir -p ~/lab/b10/regex
cd ~/lab/b10/regex
printf 'error\nerrors\nerror7\nerror.log\nerro\nwarning\n' > samples.txt
touch error.txt errors.txt erro.txt warning.txt

The text file is the input for every regex example. The empty files exist only for the shell-glob comparison. Use Reset this lesson before repeating the setup.

Terminal window
cat -n samples.txt
Captured output
1 error
2 errors
3 error7
4 error.log
5 erro
6 warning

cat -n numbers all input lines for inspection. The searches below use grep -n to identify selected source lines, as in B10.2. Neither command writes those number prefixes into samples.txt.

A regular expression, or regex, describes text to match. These examples use grep’s default basic regular expressions. No -E or other regex mode is needed for the operators taught here.

Start with a boundary:

Terminal window
grep -n '^error' samples.txt
Captured output
1:error
2:errors
3:error7
4:error.log

^ anchors the match to the start of a line. It matches a position, not a printed character. Text may still follow error, so the suffixed entries are selected too.

Predict first: which of those lines will remain if you also require the line to end after error?

Terminal window
grep -n '^error$' samples.txt
Run it, then compare with your guess
Captured output
1:error

Only the first line remains. $ anchors the end. With both anchors, the pattern accepts exactly error, without additional characters before or after it.

Match a start or an entire line
learner@rootvolt:~/lab/b10/regex$ grep -n '^error' samples.txt1:error
2:errors
3:error7
4:error.loglearner@rootvolt:~/lab/b10/regex$ grep -n '^error$' samples.txt1:errorlearner@rootvolt:~/lab/b10/regex$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The start anchor accepts four error-prefixed lines; adding the end anchor leaves only the complete word error.
Steps (6)
  1. Type grep -n '^error' samples.txt
  2. Press Enter
  3. Output: 1:error 2:errors 3:error7 4:error.log
  4. Type grep -n '^error$' samples.txt
  5. Press Enter
  6. Output: 1:error

An unanchored regex can match a portion of a line. A whole line is printed once it contains a match; the printed line and the matched portion need not have the same length.

Select one character or repeat the previous one

Section titled “Select one character or repeat the previous one”
Terminal window
grep -n '[0-9]' samples.txt
Captured output
3:error7

The bracket expression [0-9] selects one digit in the tested lab locale. A character class describes a set of possible characters at one position. Here the only selected line contains 7. The pattern does not require the whole line to be a single digit because it has no anchors.

The named class [[:digit:]] is another way to request a digit. Bracket ranges and character classes can depend on locale; these examples are verified under the course’s en_US.UTF-8 locale.

Now repeat a particular character:

Terminal window
grep -n '^errors*$' samples.txt
Captured output
1:error
2:errors

* means zero or more repetitions of the preceding item. Here that item is s, so both error and errors match. The error line uses zero repetitions. The anchors reject the extra digit and dot suffixes. This pattern would also accept more trailing s characters; this fixture contains only the two shown cases.

Select a digit or repeat the preceding character
learner@rootvolt:~/lab/b10/regex$ grep -n '[0-9]' samples.txt3:error7learner@rootvolt:~/lab/b10/regex$ grep -n '^errors*$' samples.txt1:error
2:errorslearner@rootvolt:~/lab/b10/regex$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The digit class selects error7; repeating s zero or more times selects error and errors as complete lines.
Steps (6)
  1. Type grep -n '[0-9]' samples.txt
  2. Press Enter
  3. Output: 3:error7
  4. Type grep -n '^errors*$' samples.txt
  5. Press Enter
  6. Output: 1:error 2:errors

Do not read * as a freestanding instruction to accept anything. It modifies the item immediately before it. A dot, ., is a regex item that matches any one character in these ordinary lines; .* therefore allows zero or more such characters.

A regex dot has a special meaning. To require the actual dot in error.log, put a backslash before it:

Terminal window
grep -n '^error\.log$' samples.txt
Captured output
4:error.log

Single quotes preserve the backslash for grep. Bash removes the quote characters; grep receives the regex containing \. and interprets that pair as a literal dot. Without the backslash, the dot position could accept a different character.

Escape a literal dot
learner@rootvolt:~/lab/b10/regex$ grep -n '^error\.log$' samples.txt4:error.loglearner@rootvolt:~/lab/b10/regex$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The escaped dot and both anchors select only the complete line error.log.
Steps (3)
  1. Type grep -n '^error\.log$' samples.txt
  2. Press Enter
  3. Output: 4:error.log
Require the complete line error.log The pattern anchors the start, matches error, requires a literal dot, matches log, and anchors the end. ^ Start of line error Literal letters \. Literal dot log Literal letters $ End of line
Require the complete line error.log The pattern anchors the start, matches error, requires a literal dot, matches log, and anchors the end. ^ Start of line error Literal letters \. Literal dot log Literal letters $ End of line
In ^error\.log$, the anchors require a whole line and the backslash makes the dot literal.

In words: require the start, match error, require one literal dot, match log, then require the end. The quotes used at the shell prompt protect this regex; they are not part of it.

Predict first: in a regex, does error* mean “the word error followed by anything,” as it would in a filename glob?

Terminal window
printf '%s\n' error*
grep -n 'error*' samples.txt
Run it, then compare with your guess
Captured output
errors.txt
error.txt
Captured output
1:error
2:errors
3:error7
4:error.log
5:erro

No. Bash expands the unquoted glob into existing filenames beginning with error. In the quoted regex, * repeats the final r. The line erro matches with zero copies of that r. Unanchored matching also accepts a matching portion of the longer lines.

Compare a shell glob with a grep regex
learner@rootvolt:~/lab/b10/regex$ printf '%s\n' error*errors.txt
error.txtlearner@rootvolt:~/lab/b10/regex$ grep -n 'error*' samples.txt1:error
2:errors
3:error7
4:error.log
5:errolearner@rootvolt:~/lab/b10/regex$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Bash's error* glob selects filenames, while grep's error* regex also matches the text erro because its final r may occur zero times.
Steps (6)
  1. Type printf '%s\n' error*
  2. Press Enter
  3. Output: errors.txt error.txt
  4. Type grep -n 'error*' samples.txt
  5. Press Enter
  6. Output: 1:error 2:errors 3:error7 4:error.log 5:erro

The same symbol belongs to different pattern languages. A quoted find -name 'error*' uses a filename glob interpreted by find. A quoted grep 'error*' samples.txt uses a regex interpreted by grep. Quotes protect the argument from Bash; they do not choose the pattern language for the receiving command.

The following tables cover every line for the seven recorded valid searches, including the challenge, one table per pattern. Match rows correspond exactly to source line numbers printed in the capture; no-match rows are the remaining lines of the same inspected file. These static tables and the verified replays replace an interactive matcher; the page does not execute a pattern you type.

Pattern ^error

Sample line Result
1: error Match. Starts with error.
2: errors Match. Starts with error.
3: error7 Match. Starts with error.
4: error.log Match. Starts with error.
5: erro No match. The final r is missing.
6: warning No match. Starts with warning.

Pattern ^error$

Sample line Result
1: error Match. Exactly error.
2: errors No match. Extra s before the end.
3: error7 No match. Extra digit before the end.
4: error.log No match. Extra .log before the end.
5: erro No match. The final r is missing.
6: warning No match. Different letters.

Pattern [0-9]

Sample line Result
1: error No match. No digit.
2: errors No match. No digit.
3: error7 Match. Contains the digit 7.
4: error.log No match. No digit.
5: erro No match. No digit.
6: warning No match. No digit.

Pattern ^errors*$

Sample line Result
1: error Match. Zero copies of s.
2: errors Match. One copy of s.
3: error7 No match. 7 is not a repeated s.
4: error.log No match. Dot suffix is not repeated s.
5: erro No match. The required r is missing.
6: warning No match. Different letters.

Pattern ^error\.log$

Sample line Result
1: error No match. Missing literal .log.
2: errors No match. Missing literal .log.
3: error7 No match. Missing literal .log.
4: error.log Match. Exact letters and literal dot.
5: erro No match. Missing r and literal .log.
6: warning No match. Different letters.

Pattern error*

Sample line Result
1: error Match. One final r.
2: errors Match. Matches the error portion.
3: error7 Match. Matches the error portion.
4: error.log Match. Matches the error portion.
5: erro Match. Zero copies of the final r.
6: warning No match. No erro portion.

Pattern ^error[0-9]$

Sample line Result
1: error No match. Missing a digit.
2: errors No match. s is not a digit.
3: error7 Match. Exactly error plus one digit.
4: error.log No match. Dot suffix is not one digit.
5: erro No match. Missing r and a digit.
6: warning No match. Different letters.

Leaving a bracket expression unfinished.

Terminal window
grep '[' samples.txt
Captured output
grep: Invalid regular expression

The diagnostic says the regular expression is invalid; it does not identify the bracket. Here [ begins an unfinished bracket expression. Close it, for example [0-9], before judging its matches. An invalid regex is different from a valid pattern that selects nothing.

Expecting quotes to make regex punctuation literal. Single quotes protect a pattern from Bash. grep still interprets its regex operators. Escape the regex dot when you mean a dot.

Using a filename wildcard as a regex. Decide which program receives the pattern and which language that program uses. The real comparison above shows why the distinction changes the result.

Goal: Select only a complete line consisting of error followed by exactly one digit. Keep its source line number. Do not select error, errors, or error.log.

Hint

Use both boundaries and a single digit class. Repetition is not needed for exactly one digit.

Solution
Terminal window
grep -n '^error[0-9]$' samples.txt
Captured output
3:error7

The class supplies one character. Both anchors prevent extra characters around the requested structure.

Terminal window
cd ~/lab/b10
ls -AR regex
Terminal window
rm -r ~/lab/b10/regex

Rerun Before you start. If the directory is already absent, skip removal.

  • ^ and $ anchor the start and end of a line.
  • Bracket expressions select one character; * repeats the preceding item zero or more times.
  • A backslash escapes regex punctuation such as a literal dot.
  • Shell filename globs and grep regex use different rules. Quoting preserves the argument for the program that interprets it.

Next, Combining searches chooses a tool and connects the steps to a concrete question.