Linux Labs

Bash: text processing and scripts / Stage 1 of 9

Regular expressions in grep

Not running Starting usually ~20 s
Leave the lesson

Step 2 of 6 · on a machine · 6 min

A match anywhere in the line

The search found something other than what you were after. This step shows how grep compares a pattern with a line and how to tell it where a match should sit.

grep checks every line: do the pattern's characters appear in a row — at the start, in the middle, inside another word. The pattern error matched the end of the word terror, and err matched part of the words deferred and sherry.

A grep pattern is a regular expression. Letters and digits in it stand for themselves, and some characters set where the match must sit:

Pattern What it finds
^INFO lines that start with INFO
ms$ lines that end in ms

The -w option requires the match to be a whole word: on its left and right there is the edge of the line or a character that is not a letter, a digit or _. That is why grep -w error app.log will not print the terror line. Try -w on the short pattern err.

Exercise · look

Search for err as a whole word

In ~/grep-lab, find the lines of app.log where err is a whole word, ignoring case.

First, predict

Which lines will grep -iw err app.log print?

Now start the exercise, run the command and compare your prediction with the output.

Sign in and start The machine is available after signing in
Ubuntu 24.04
Loading…