How do sort and uniq work together?

Answer

sort file sorts lines alphabetically by default. Key options: -n (numeric sort), -r (reverse), -k 2 (sort by the second field), -t: (use colon as field delimiter), -u (output unique lines only). uniq removes or reports adjacent duplicate lines — it only detects consecutive duplicates, so always pipe through sort first: sort file | uniq. uniq -c prefixes each line with its occurrence count — combined with sort -rn this gives you a frequency table: awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -20 shows the 20 most frequent IP addresses.