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

← All Scripting (Bash, Groovy) questions