$_ CLI In Depth
Learning progress 0%
build text-processing regex awk

Build: An Access-Log Analysis Pipeline

5 min read Module 6: Text Processing: grep, sed & awk Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:15

The input you will analyze

Most web servers write one line per request in a common format. In the common log layout, the first field is the client address, the eighth field is the HTTP status code, and the sixth field is the request path, counting fields from one with the default whitespace splitting. The request itself sits in double quotes, so it splits into three fields: the method, the path, and the protocol. The sample below is a shortened version of that layout. The lab creates a file like it for you, and your script must work on any file with the same shape.

TEXT1 lines
203.0.113.5 - - [05/Dec/2026:10:00:01 +0000] "GET /index.html HTTP/1.1" 200 512

Notice the time zone inside the brackets. It contains a space, so the timestamp occupies two fields, and the status code lands in field 9. Without the zone the counts would shift by one column.

Real logs have quirks, so the script has to be defensive. Some lines are blank, some requests have status codes that are not 200, and some client addresses repeat across many lines. Your report must count correctly in all of those cases.

The specification

Write a script named report.sh in your lab home directory. It takes one argument, the path of the log, and prints three sections in this order. First, the title Top clients and the five most frequent client addresses with their counts, highest first. Second, the title Status codes and one line per status code with its count, sorted by the code. Third, the title Top paths and the three most requested paths, highest count first.

The script must not modify the log. It must print an error and exit with status 2 when the argument is missing or the file cannot be read. Use grep, cut or awk, sort, uniq or awk arrays, and head, choosing the tool that fits each question. Each section should be a pipeline you can test on its own before you join them together.

Design notes

Start by testing each question on its own. For the clients, the first field is enough, so a cut or awk command selects it, and sort plus uniq -c with a numeric reverse sort ranks the results. For the status codes, the eighth field is the code, and the output must be sorted by the code itself, which is a different key from the count. For the paths, the sixth field contains the path, and the same ranking pattern applies.

Blank lines and lines with too few fields are the usual trap. A blank line makes awk print an empty field and cut print nothing useful. Filter them with a pattern such as NF >= 9 in awk, or with grep that requires a non-empty line, before you count anything.

Tip

Write the report as a single awk program only after the pipeline versions work. A pipeline of small steps is easier to debug, and the awk version is a useful cross-check of the numbers.

A reference solution

The reference below is one correct way to write the script. Read it after you have tried the task yourself, and compare the output with your own version on the same log.

BASH10 lines
#!/bin/bash
[ $# -eq 1 ] || { echo "usage: report.sh LOGFILE" >&2; exit 2; }
log=$1
[ -r "$log" ] || { echo "cannot read $log" >&2; exit 2; }
echo "Top clients"
awk 'F >= 9 { print $1 }'"$log" | sort | uniq -c | sort -rn | head -5
echo "Status codes"
awk 'F >= 9 { print $8 }'"$log" | sort | uniq -c | sort -k2,2n
echo "Top paths"
awk 'F >= 9 { print $6 }'"$log" | sort | uniq -c | sort -rn | head -3

The script checks its argument and readability first, then runs three pipelines. Each awk filter keeps only lines with at least nine fields, so blank and short lines never reach the counters. The status section sorts by the second column numerically, which is the code, while the other two sections sort by the count.

Handling unusual lines

Real logs contain surprises. A request line may contain a quoted space, which shifts the field numbers, and a few entries may be written by a different format entirely. The safest approach is to count only lines that have the shape you expect, and to keep a count of the lines you skipped. That number is itself useful: a sudden rise in skipped lines often means the log format has changed.

Make the skip visible in the report rather than discarding lines silently. A line such as skipped N malformed lines printed to standard error tells the reader how much of the input the numbers cover. This is a small habit, and it is the difference between a report you can trust and one that merely looks tidy.

Checking your work

Test the script on a log where you know the answers. Count the lines by hand for a few clients, add a blank line and a truncated line, and confirm that the report ignores them. Run it with no argument and with a missing file, and check the exit status with echo $?. A script that handles only the happy path is not finished.

Terminal lab · 3 steps · real Linux in your browser

Build the access-log report

Produce the client and status tables, then write report.sh with its checks.

01
Rank the top clients
access.log is a common-format web log with a time zone in the timestamp, so the client address is field 1, the status is field 9, and the path is field 7. Save the top clients with their counts, skipping short or blank lines: ``bash awk 'NF >= 9 { print $1 }' access.log | sort | uniq -c | sort -rn | head -5 > clients.txt ``
Show hint for step 1
Filter with NF >= 9 first, then sort, count, and rank by count.
02
Count status codes in numeric order
Count each status code, and sort the result by the code itself, not by the count: ``bash awk 'NF >= 9 { print $9 }' access.log | sort | uniq -c | sort -k2,2n > status.txt ``
Show hint for step 2
The code is the second column after uniq -c, so the sort key is -k2,2n.
03
Write report.sh with usage checks
Write report.sh in /workspace. It takes one log path, prints the headings Top clients, Status codes, and Top paths with their tables, and exits with status 2 when the argument is missing or unreadable. Use the pipelines from the previous steps as the core of each section. Run it on access.log to test it: ``bash bash report.sh access.log ``
Show hint for step 3
Check $# and -r before doing any work, and send errors to standard error with >&2.
Check yourself Question 1 of 4
Concept Check Usage error status recall

According to the build spec, which exit status should report.sh use when the log argument is missing or unreadable?

Recap & Key Takeaways

A correct report needs filtering first, so that blank and short lines never reach the counters.
Each section is a pipeline of extract, sort, count with uniq -c, and rank with sort -rn, and status codes need a sort by the code itself.
Validate the argument and readability before doing work, and return status 2 on usage errors.
Test each section separately, then combine them, and check edge cases by hand.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue