Lessons · Terminal · sort -u: the distinct values
The distinct values
-u sorts and drops repeats in one pass. It answers WHICH values, not how many of each.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
"How many customers" and "how many orders" are the same shape and different questions.
How to think about it
sort -u to list them; sort -u | wc -l to count them; sort | uniq -c when you need the counts.
Worked example
cut -d' ' -f1 access.log | sort -uThree names: ada, bo, cy.
cut -d' ' -f1 access.log | sort -u | wc -l3 -- how many people.
cut -d' ' -f1 access.log | wc -l7 -- how many requests. A different question with the same shape.
Your turn
List the distinct lines of app.log.
sort app.log
Try it at a real prompt
The trap
Reaching for -u when you were asked for counts throws the answer away silently: the output still looks like an answer.
Practise sort -u: the distinct values on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.