Linux Labs

First steps / Stage 9 of 15

Text and editors

Not running Starting usually ~20 s
Leave the lesson

Step 3 of 8 · on a machine · 6 min

Searching for lines

The engineer's search stayed silent although the log had errors. Here is how grep compares letters and what its empty output means.

grep prints the lines of a file that contain a pattern. It compares letters exactly: error and ERROR are different words to it, hence the empty output at the start of the lesson. Options change this:

Option What it does
-i letter case does not matter
-n adds the line number
-c prints only the number of matching lines

Empty output means one thing: there are no lines with this pattern. The word may be spelled differently, or you may be searching the wrong file. If the file is missing or unreadable, grep says so: No such file or directory or Permission denied.

A grep pattern is a regular expression, with rules of its own, unlike a filename pattern such as *.tmp: a dot means "any character", ^ at the start means "start of line", and square brackets mean "one of the characters inside".

To search for text literally, such as [ERROR], add -F. You can save what you find with > from the lesson on paths: grep -i error service.log > errors.txt.

Exercise · look

Find every error and what came next

In ~/text-lab, find the error lines together with their numbers, then look at the last three lines of the log.

First, predict

How many lines will grep -n -i error service.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…