|, sort, uniq -c & wc -l

Combine commands with pipes

Lesson 10 of 20 · 10 min · Module 3: Text superpowers

Each Linux command does one small job well. A pipe connects them so the output of one becomes the input of the next.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Connect commands with a pipe
  • Count distinct and repeated lines
  • Build a pipeline one step at a time

The idea

The | symbol is a pipe. sort orders lines, uniq collapses repeated neighbouring lines (so sort first), uniq -c counts them, and wc -l counts lines. sort -rn sorts numbers from highest to lowest.

sort data/users.txt | uniq
sort data/users.txt | uniq -c | sort -rn

01A pipe passes text along

In sort users.txt | uniq, sort does not print to the screen. Its output flows into uniq, which prints the final result.

02uniq needs sorted input

uniq only merges identical lines that are next to each other. Sorting first brings all the copies of a name together.

sort data/users.txt | uniq -c
Output
      3 alex
      1 jordan
      1 mia
      2 sam

03Grow a pipeline gradually

Run the first command, check the output, then add the next | step. When something looks wrong, remove the last step to see where the data changed.

Your turn

data/users.txt lists a user name for every login. Show how many different users logged in, as a single number. Then show the most active user and their count, as one line, using head -n 1.

What the checks look for

  • Counts 4 different users
  • Shows alex with 3 logins as the top line
Try it in the editor →

Take this with you

Small commands joined with pipes answer surprisingly big questions.

Go a little further

Find the three most common log levels in logs/app.log with cut, sort, uniq -c, and sort -rn.

Learn Linux Command Line for free

20 short lessons with real code and instant checks.

See the whole course →