Course outline
Course / The terminal and the language

See what happened around a failure

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-04-grep-context-around-errors
cd 02-04-grep-context-around-errors
gonomicon submit

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

Goal

A single log line rarely explains a failure; the lines around it do. Show every line of testdata/app.log that contains timeout, together with the line just before and just after it.

Example

For this log:

start
timeout here
middle
more
end

your script prints:

start
timeout here
middle

(Lines not near a match are left out. If two groups of lines are separated by a gap, grep prints a -- line between them. Keep it.)

Rules

  • Edit answer.sh. One grep command is enough.
  • If nothing matches, print nothing.

What you'll practice

grep -C (context lines).

Run it

gonomicon fetch week-1/02-04-grep-context-around-errors
cd 02-04-grep-context-around-errors
gonomicon test
gonomicon submit

Hints

  1. One line of context on each side of a match is -C 1.
  2. grep prints -- between groups that are not next to each other. Leave it in.
  3. grep -C 1 "timeout" testdata/app.log

Something wrong with this lesson? Report a problem