Hone

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 -u
Three names: ada, bo, cy.
cut -d' ' -f1 access.log | sort -u | wc -l
3 -- how many people.
cut -d' ' -f1 access.log | wc -l
7 -- how many requests. A different question with the same shape.

Your turn

List the distinct lines of app.log.

sort  app.log

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.