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 purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]