Three commands that look trivial on their own and become a real tool the moment you chain them.
The pieces
sort puts lines in alphabetical order.
sort visitors.txt
anna
anna
anna
boris
boris
carl
uniq removes repeated lines — but only lines that are next to each other.
wc counts. wc -l for lines, -w for words, -c for characters.
Important
That “next to each other” is the trap. Run uniq on the unsorted file and it removes almost nothing, because the duplicates are scattered. uniq compares each line only with the one immediately before it.
Quick check
Your file is anna, boris, anna. What does uniq alone remove?
The combination
sort visitors.txt | uniq
anna
boris
carl
Sort brings the duplicates together, uniq collapses them, and you get the distinct values. This pair is one of the most-typed things in Unix.
Add a count:
sort visitors.txt | uniq | wc -l
3
Three distinct visitors, from six lines of raw data — and you never opened a spreadsheet.
Quick check
Why does `sort file | uniq | wc -l` give a different number from `wc -l file`?
Save it instead of printing it
Level 2's works at the end of a pipeline just as well as after a single command:
sort visitors.txt | uniq > unique-visitors.txt
The text flows through sort, then uniq, and the final result lands in a new file. visitors.txt is untouched — nothing in this chain modifies its input.
Tip
sort -u does the sort-and-dedupe in one command. Both forms are everywhere; sort | uniq is worth learning first because it shows you the two separate ideas.
Quick check
After `sort visitors.txt | uniq > clean.txt`, what happened to visitors.txt?
Try it
visitors.txt has six lines and three names. Build the pipeline one stage at a time so you can see each one working:
cat visitors.txt— the raw data, unsorted.sort visitors.txt— same six lines, grouped.sort visitors.txt | uniq— three lines.sort visitors.txt | uniq | wc -l— the number 3.sort visitors.txt | uniq > unique-visitors.txt— save it.cat unique-visitors.txt— check what landed.
That is the whole method: watch each stage, then add the next one. You now have every piece needed to answer real questions about real files.