$_ CLI In Depth
Learning progress 0%
Build · Text Processing: grep, sed & awk

Build: An Access-Log Analysis Pipeline

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

~40 min Beginner 3 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

What you will build

Step 1: 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:

BASH1 lines
awk 'NF >= 9 { print $1 }' access.log | sort | uniq -c | sort -rn | head -5 > clients.txt

Step 2: Count status codes in numeric order

Count each status code, and sort the result by the code itself, not by the count:

BASH1 lines
awk 'NF >= 9 { print $9 }' access.log | sort | uniq -c | sort -k2,2n > status.txt

Step 3: 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:

BASH1 lines
bash report.sh access.log

Files

  • walkthrough.sh: every step of the reference solution, in order.

How it is checked

Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

walkthrough.sh

BASH28 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: Rank the top clients
# (the lab prepares this first)
# printf '%s\n' '203.0.113.5 - - [05/Dec/2026:10:00:01 +0000] "GET /index.html HTTP/1.1" 200 512' '203.0.113.5 - - [05/Dec/2026:10:00:02 +0000] "GET /about.html HTTP/1.1" 200 128' '198.51.100.7 - - [05/Dec/2026:10:00:03 +0000] "GET /index.html HTTP/1.1" 200 512' '198.51.100.7 - - [05/Dec/2026:10:00:04 +0000] "POST /login HTTP/1.1" 302 0' '203.0.113.5 - - [05/Dec/2026:10:00:05 +0000] "GET /missing HTTP/1.1" 404 0' '192.0.2.9 - - [05/Dec/2026:10:00:06 +0000] "GET /index.html HTTP/1.1" 200 512' '' '203.0.113.5 - - [05/Dec/2026:10:00:08 +0000] "GET /index.html HTTP/1.1" 200 512' > access.log
awk 'NF >= 9 { print $1 }' access.log | sort | uniq -c | sort -rn | head -5 > clients.txt

# Step 2: Count status codes in numeric order
# (the lab prepares this first)
# printf '%s\n' '203.0.113.5 - - [05/Dec/2026:10:00:01 +0000] "GET /index.html HTTP/1.1" 200 512' '203.0.113.5 - - [05/Dec/2026:10:00:02 +0000] "GET /about.html HTTP/1.1" 200 128' '198.51.100.7 - - [05/Dec/2026:10:00:03 +0000] "GET /index.html HTTP/1.1" 200 512' '198.51.100.7 - - [05/Dec/2026:10:00:04 +0000] "POST /login HTTP/1.1" 302 0' '203.0.113.5 - - [05/Dec/2026:10:00:05 +0000] "GET /missing HTTP/1.1" 404 0' '192.0.2.9 - - [05/Dec/2026:10:00:06 +0000] "GET /index.html HTTP/1.1" 200 512' '' '203.0.113.5 - - [05/Dec/2026:10:00:08 +0000] "GET /index.html HTTP/1.1" 200 512' > access.log
awk 'NF >= 9 { print $9 }' access.log | sort | uniq -c | sort -k2,2n > status.txt

# Step 3: Write report.sh with usage checks
# (the lab prepares this first)
# printf '%s\n' '203.0.113.5 - - [05/Dec/2026:10:00:01 +0000] "GET /index.html HTTP/1.1" 200 512' '203.0.113.5 - - [05/Dec/2026:10:00:02 +0000] "GET /about.html HTTP/1.1" 200 128' '198.51.100.7 - - [05/Dec/2026:10:00:03 +0000] "GET /index.html HTTP/1.1" 200 512' '198.51.100.7 - - [05/Dec/2026:10:00:04 +0000] "POST /login HTTP/1.1" 302 0' '203.0.113.5 - - [05/Dec/2026:10:00:05 +0000] "GET /missing HTTP/1.1" 404 0' '192.0.2.9 - - [05/Dec/2026:10:00:06 +0000] "GET /index.html HTTP/1.1" 200 512' '' '203.0.113.5 - - [05/Dec/2026:10:00:08 +0000] "GET /index.html HTTP/1.1" 200 512' > access.log
cat > report.sh <<&#039;EOF&#039;
#!/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 &#039;NF >= 9 { print $1 }&#039; "$log" | sort | uniq -c | sort -rn | head -5
echo "Status codes"
awk &#039;NF >= 9 { print $9 }&#039; "$log" | sort | uniq -c | sort -k2,2n
echo "Top paths"
awk &#039;NF >= 9 { print $7 }&#039; "$log" | sort | uniq -c | sort -rn | head -3
EOF

Lessons this build uses