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

You have access logs from five web servers and need one timeline sorted by time. How?

IntermediateScenarioScenarios: text processing and scheduling decisions
Answer

Situation read → Each file is already sorted, so merge rather than re-sort, and tag lines with their source.

What I'd do →

  1. If logs use ISO 8601 timestamps at line start, a merge sort is enough:
    for h in web{1..5}; do ssh -n "$h" 'cat /var/log/app/app.log' | sed "s/^/$h /" > "/tmp/$h.log"; done
    sort -m -k2,2 /tmp/web*.log > timeline.log
    
  2. For Nginx's [09/Nov/2026:14:23:01 +0000] format, convert to a sortable key first (epoch via gawk mktime), then sort numerically.
  3. Make sure clocks were synchronized, or the merged timeline lies.

Why → sort -m is linear on pre-sorted input; ISO timestamps sort correctly as strings.

Interview tip → Say this is the argument for ISO 8601 UTC timestamps everywhere and for central logging.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: text processing and scheduling decisions