Interview Prep/Shell Scripting/Scenarios: text processing and scheduling decisions

Users report errors in the last hour. From the Nginx access log, find which endpoints produce the most 5xx responses.

BeginnerScenarioScenarios: text processing and scheduling decisions
Answer

Situation read → Filter by time and status, strip query strings, count per path.

What I'd do →

since=$(date -d '1 hour ago' '+%d/%b/%Y:%H')
awk -v s="$since" '$4 >= "["s && $9 ~ /^5/ {split($7, p, "?"); print $9, p[1]}' /var/log/nginx/access.log |
  sort | uniq -c | sort -rn | head -n 15
  1. Field 9 is the status, field 7 the request path in the combined format.
  2. Dropping the query string groups /api/orders?id=1 and ?id=2 together.
  3. The string comparison on $4 works within a single day; for windows across midnight, use epoch conversion or journalctl --since if logs go to the journal.

Why → Grouping by endpoint turns 50,000 error lines into "it is /api/checkout".

Interview tip → Mention checking $upstream_status or upstream response time fields too, to separate nginx errors from backend errors.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: text processing and scheduling decisions