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

You need to scan 50 GB of compressed logs for a request ID during an incident, as fast as possible. How?

AdvancedScenarioScenarios: text processing and scheduling decisions
Answer

Situation read → I/O and decompression are the bottlenecks; parallelize across files and use the fastest matching mode.

What I'd do →

find /var/log/app -name '*.gz' -print0 |
  xargs -0 -P "$(nproc)" -n 4 zgrep -F -H -- 'req-7f3a9c' > /tmp/hits.txt
  1. -F fixed-string matching is much faster than regex; LC_ALL=C speeds it up further.
  2. -P $(nproc) runs one decompressor per core; -n 4 batches files.
  3. Narrow by time first (file names or mtime) to read less data.
  4. If installed, rg -z -F (ripgrep) searches compressed files in parallel by itself.
  5. Run with nice on a production host, or copy logs elsewhere.

Why → Parallel decompression typically scales nearly linearly with cores until disk throughput becomes the limit.

Interview tip → Mention that this is the moment you wish logs were in a central, indexed system, and say how you would make that case after the incident.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: text processing and scheduling decisions