Skip to content
zero2vibecodelearn vibe coding
Level 3 · Search and pipes 6 min Reviewed by the author · Aug 4, 2026

sort, uniq and wc together

Three small commands that only become useful in combination: sort orders lines, uniq collapses repeats, wc counts them.

Level 3 · 0 / 4 lessons0%

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:

  1. cat visitors.txt — the raw data, unsorted.
  2. sort visitors.txt — same six lines, grouped.
  3. sort visitors.txt | uniq — three lines.
  4. sort visitors.txt | uniq | wc -l — the number 3.
  5. sort visitors.txt | uniq > unique-visitors.txt — save it.
  6. 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.

Check your understanding

Answered 0/3
Mode:

Why does `uniq` almost always come after `sort`?

What does `wc -l` count?

What does `sort names.txt | uniq > clean.txt` leave you with?

Finish the task in the terminal and answer the questions to complete the lesson.

📝 My notes

Saved automatically in this browser · shows up on the Notes page.

Task

Turn a list with duplicates into a clean list of unique names, count it, and save it.

Tab complete · history · Ctrl+L clear · help commands

  • Not done: Pipe sort into uniq
  • Not done: Count the result with wc -l
  • Not done: The clean list was saved
  • Not done: The saved file holds the unique names

Lesson Q&A

Why does `uniq` almost always come after `sort`?

uniq compares each line with the one immediately before it. Sorting first is what brings the duplicates together.

What does `wc -l` count?

l is for lines. -w counts words and -c counts characters; with no flag you get all three.

What does `sort names.txt | uniq > clean.txt` leave you with?

A pipe carries the text through both stages, and the redirect at the end writes the final result to a new file. The original is untouched.

Your file is anna, boris, anna. What does uniq alone remove?

uniq only ever looks at the previous line. Sorting is what puts the duplicates side by side.

Why does `sort file | uniq | wc -l` give a different number from `wc -l file`?

uniq removes duplicates before the counting happens, so you get 'how many different' rather than 'how many'.

After `sort visitors.txt | uniq > clean.txt`, what happened to visitors.txt?

sort and uniq read and print. The only thing written here is clean.txt, because of the redirect.

Move between lessons with · search with ⌘K