AI Hints
Home›Linux Tutorial›Patterns and Practice
Linux · Lesson 59 of 120

Practice: A Log Report

Pipelines in action.

⏱ 2 min read▶ Video at 1:16:26🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Your manager wants a quick report from the server log. How many requests were there? Which pages had errors? And who had the most errors? Let's answer each one with a pipeline. Pause and try first.

Practice: A Log Report
From the video: Practice: A Log Report
  • A report for your manager
  • Three questions
  • One pipeline each

Requests and error pages

First, WC dash L counts every request. Sixty. Then grep finds the errors, awk prints the page, which is the sixth field, and sort with uniq dash C counts each page. The home page had the most errors.

Terminal
wc -l < logs/server.log
60
grep ERROR logs/server.log | awk '{print $6}' | sort | uniq -c
      1 /cart
      4 /home
      2 /shop

Who had the most errors

Same idea, but with field four, the user. And a final sort dash RN puts the biggest first. Priya had four of the seven errors. That's someone to help. And we can save the whole report into a file with a redirect, to send to the manager.

Terminal
grep ERROR logs/server.log | awk '{print $4}' | sort | uniq -c | sort -rn
      4 priya
      2 alex
      1 jo
grep ERROR logs/server.log | awk '{print $4}' | sort | uniq -c | sort -rn > report.txt

The pattern behind it

Almost every report like this follows the same steps. Filter the lines you care about, with grep. Pick the column, with awk or cut. Group and count, with sort and uniq dash C. Then rank the results, with sort dash RN. Learn this shape, and you can answer questions about almost any log.

keyvalue
Filtergrep
Pickawk / cut
Countsort | uniq -c
Ranksort -rn

Summary

Filter, pick, count, rank. That one pipeline shape answers most questions about a log. Next, one more text challenge to finish this section.

Key takeaways
  • grep to filter
  • awk to pick
  • sort | uniq -c | sort -rn
Next up: Practice: Text Challenge — tr, sed and friends