Course outline
Course / The terminal and the language

Find lines with grep

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each lab: download it, edit the file named in the instructions, then submit.

gonomicon fetch week-1/02-02-grep-case-and-line-numbers
cd 02-02-grep-case-and-line-numbers
gonomicon submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal

In testdata/app.log, find every line that contains the word "error" in any letter case (error, Error, ERROR). First print how many such lines there are, then print the lines themselves with their line numbers.

Example

For a log like this:

all fine
ERROR disk full
still fine
Error: again

your script prints:

2
2:ERROR disk full
4:Error: again

Rules

  • Edit answer.sh. Print the count first, on its own line, then each matching line as NUMBER:the line.
  • If nothing matches, print only 0.
  • Use grep; do not count by hand.

What you'll practice

grep -i, grep -n, and grep -c.

Run it

gonomicon fetch week-1/02-02-grep-case-and-line-numbers
cd 02-02-grep-case-and-line-numbers
gonomicon test
gonomicon submit

Hints

  1. You need grep twice: once to count, once to show the lines.
  2. -i ignores letter case, -c counts, -n adds line numbers.
  3. grep -i -c "error" FILE then grep -i -n "error" FILE.

Something wrong with this lesson? Report a problem