# sort, uniq and wc together

Course: Terminal Basics — https://zero2vibecode.com/learn/terminal-basics
Canonical URL: https://zero2vibecode.com/learn/terminal-basics/3-4-sort-uniq-wc

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

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.

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.

## 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.

## Save it instead of printing it

Level 2's redirect 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.

`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.

<MicroQuiz
  question="After `sort visitors.txt | uniq > clean.txt`, what happened to visitors.txt?"
  options="Nothing — these commands read their input, they never rewrite it | It was sorted in place | It was replaced by clean.txt"
  answer="0"
  explanation="sort and uniq read and print. The only thing written here is clean.txt, because of the redirect."
/>

## 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.
