Hone

Lessons · Terminal · how many rows, how many things

How many rows, how many things

Counting lines and counting distinct values are the same shape and different questions.

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

Answering one when you were asked the other is the commonest way a pipeline is confidently wrong.

How to think about it

Say the question out loud first. "How many orders" is wc -l; "how many customers" needs sort -u in the middle.

Worked example

wc -l < access.log
7 requests in the file.
cut -d' ' -f1 access.log | sort -u | wc -l
3 distinct people made them.
cut -d' ' -f2 access.log | sort -u | wc -l
3 endpoints between them.

Your turn

Count the distinct lines of app.log.

sort -u app.log |  -l

The trap

Both numbers are correct and only one answers the question. Nothing in the output tells you which you asked for.

Practise how many rows, how many things on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.