AI Hints
Home›Linux Tutorial›cron and First Projects
Linux · Lesson 111 of 120

Project: Log Report

A summary in one command.

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

Every morning, someone has to look at the server log. How many errors? Who had them? Which page is busiest? Let's turn the text tools you know into a script that answers it all.

Project: Log Report
From the video: Project: Log Report
  • How many errors?
  • Who had them?
  • Which page is busiest?

report.sh

The log file is the first argument. WC counts the lines, and grep dash C counts the errors. Then grep, awk, sort, and unique count errors for each user. Finally, awk takes the sixth field, the page, and the top count is the busiest page.

nano report.shbash
#!/bin/bash
set -e
log="$1"
echo "Report for $log"
echo "Lines:  $(wc -l < "$log")"
echo "Errors: $(grep -c ERROR "$log")"
echo "Errors by user:"
grep ERROR "$log" | awk '{print $4}' | sort | uniq -c | sort -rn
echo "Busiest page:"
awk '{print $6}' "$log" | sort | uniq -c | sort -rn | head -n 1

The report

One command, and the report is ready. Sixty lines, seven errors, Priya had the most, and the busiest page. Add a redirect, and it's saved to a file, ready to email.

Terminal
./report.sh logs/server.log
Report for logs/server.log
Lines:  60
Errors: 7
Errors by user:
      4 priya
      2 alex
      1 jo
Busiest page:
     19 /home
./report.sh logs/server.log > report.txt && wc -l report.txt
9 report.txt

Make it yours

This is a pattern you'll use again and again. A script that takes a file, runs a few pipelines, and prints a clean summary. Add it to cron, and the report is waiting for you every morning, before you even start work.

keyvalue
$1which log
grep -ccount matches
sort | uniq -ccount each value
cronevery morning

Summary

Our log report counts lines and errors, ranks users, and finds the busiest page, all in one script. Next, a project that checks the health of a computer.

Key takeaways
  • grep -c ERROR
  • awk | sort | uniq -c
  • sort -rn | head -n 1
Next up: Project: Health Check — is everything OK?