From JSON Lines application logs, count unique users per day.
IntermediateScenarioScenarios: text processing and scheduling decisions
Answer
Situation read → Extract (date, user) pairs, de-duplicate, count per date.
What I'd do →
jq -r 'select(.user_id != null) | "\(.ts[0:10]) \(.user_id)"' app-*.jsonl |
sort -u | awk '{n[$1]++} END {for (d in n) print d, n[d]}' | sort
.ts[0:10]slices the date from an ISO timestamp.sort -uremoves duplicate (date, user) pairs; awk counts per day.- For huge files,
LC_ALL=C sort -u -S 1G --parallel=4.
Why → De-duplicating pairs before counting is the shell equivalent of COUNT(DISTINCT user_id) GROUP BY day.
Interview tip → Mention handling lines that are not JSON with jq -R 'fromjson? | ...'.
More on 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.BeginnerA MySQL slow log is 3 GB. You need the 10 slowest query patterns. What do you do?IntermediateTwo servers that should be identical behave differently. Compare their installed packages.BeginnerFind which pods were OOMKilled in a namespace, with their container names and restart counts.IntermediateProduce a CSV of running EC2 instances that are missing an `Owner` tag.IntermediateYou have access logs from five web servers and need one timeline sorted by time. How?Intermediate