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 1 of 6 · on a machine · 5 min

The night log

What you will learn

  • Set where a match sits in a line — ^, $ and the -w option — and a set of characters in square brackets.
  • Combine alternatives into one pattern with grep -E and |.
  • Cut just the match out of lines with -o and count those pieces with a pipeline.

Overnight the application lost its database connection twice. To investigate, you need every line of the log app.log that records an error. The developers write them however they like: ERROR, Error: or err= with an error number, and err=0 means there was no error. Start with the search that first comes to mind.

Check yourself 1 question

Why did grep error print the line login ok user=terror?

  • grep does not look for similar words: grep errror app.log prints nothing. It looks for the pattern's characters in a row.

  • Without options, grep prints no neighbouring lines: the line before terror, with err=5, is not in the output.

  • grep does tell case apart: there are no ERROR lines in the output. The grep alias on Ubuntu only adds colour — check with alias grep.

The pattern error matched the end of the word terror.

Ubuntu 24.04
Loading…