Hone

Lessons · Terminal · uniq only sees neighbours

uniq only sees its neighbour

uniq compares each line with the one immediately before it. Nothing else.

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

On unsorted input it removes some duplicates and leaves others, and the result is plausible.

How to think about it

sort first. Every time. That is why the two commands are nearly always written together.

Worked example

uniq three.txt
Three lines in, three lines out. Nothing was adjacent.
sort three.txt
Now the two b's are next to each other.
sort three.txt | uniq
Two lines. This is the version that answers the question.

Your turn

Group app.log before uniq sees it.

 app.log | uniq

The trap

Data that arrives grouped by accident hides this bug until the day it arrives in a different order.

Practise uniq only sees neighbours on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.