Inverting, Counting, and Combining

Inverting a search is underrated. grep -v INFO app.log shows everything that is not routine information, which is usually exactly what you want from a log. You are rarely looking for the normal lines. You are looking for what is left when you remove them.

Counting answers different questions than listing. grep -c ERROR app.log tells you how many error lines there are without printing them. When the answer is 3 you probably want to read them. When it is 4000 you have learned something more useful than any individual line would have told you.

✦ The exit code is why grep works in scripts

grep exits 0 when it found something and 1 when it did not. That is what makes grep -q pattern file && echo found work, and it is why grep is the single most common command on the left of a double ampersand. Searching is not just for humans reading output, it is how scripts ask questions.

SANKOFA SANDBOX
learner:~$

logs/app.log has INFO, WARN and ERROR lines.

Try it → Run `grep -v INFO logs/app.log` to see only the interesting lines. Then `grep -c ERROR logs/app.log`. Then try `grep ERROR logs/app.log && echo 'errors found'` and see the chaining work.

◈ Graded Task

Audit the workspace. Find every TODO anywhere in the projects directory and save the results, with filenames and line numbers, into todos.txt in your home directory. Separately, count how many ERROR lines are in logs/app.log and write just that number into errors.txt.

Checked against the filesystem you leave behind, not the commands you type, so any route that gets the job done counts. 5 visible and 2 hidden checks.

SANKOFA SANDBOX
learner:~$

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back