Interview › Scripting (Bash, Groovy)
How would you find the top N most frequent values in a log (sort | uniq -c | sort -rn)? [Advanced]
Answer
To find the top N most frequent values, extract the field, sort it, count adjacent duplicates with uniq -c, sort numerically descending, and take the first N with head.
Technical explanation
uniq only counts adjacent duplicates, which is why sorting before uniq is required.
sort -rn sorts counts numerically in reverse order.This pattern is very useful for log analysis, such as top IPs, status codes, endpoints, or error messages.
Hands-on example
# Top 10 client IPs in an access log.
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
# Top HTTP status codes.
awk '{print $9}' access.log | sort | uniq -c | sort -rn
Preparing for an interview?
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What is the difference between sed -i and sed without -i? [Advanced]
- How do you use awk to sum or average a column? [Advanced]
- What is the difference between NR and NF in awk? [Advanced]
- How would you write a health-check script that polls a URL until it returns 200? [Advanced]
- What is the difference between a cron job and a systemd timer? [Advanced]
- What is idempotency, and how do you make a script safe to run repeatedly? [Advanced]
- How do you split a string on a delimiter in Bash? [Intermediate]
- How do you securely handle secrets in a shell script (avoid hardcoding, use env or a vault)? [Advanced]