Project: Log Report
A summary in one command.
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.

- 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.
#!/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 1The 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.
./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.
| key | value |
|---|---|
| $1 | which log |
| grep -c | count matches |
| sort | uniq -c | count each value |
| cron | every 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