🎓 A1Learner
🔍
0 of 50 lessons completed

Pipes

A pipe sends the output of one command to the next. A pipeline is a row of commands joined by pipes.

Sort, then keep each name once

sort puts the names in order. uniq keeps each name once.

$ cd ~/practice
$ sort data/names.txt | uniq

Sort, then count

sort puts the numbers in order. uniq -c counts each number.

$ cd ~/practice
$ sort data/numbers.txt | uniq -c

Save a copy with tee

tee prints the names and saves them in top.txt. cat shows the saved copy.

$ cd ~/practice
$ sort data/names.txt | uniq | tee top.txt
$ cat top.txt

Example

$ cd ~/practice
$ sort data/names.txt | uniq -c | sort -r

Key points

  • Use | to join two commands into a pipeline.
  • The first command prints its words, and the next command reads them.
  • Use tee to save a copy of the words in a file.
  • tee prints the words and saves them at the same time.
  • A pipeline can join more than two commands.
  • A pipe joins commands, so each one works on the words of the one before it.

In this example, sort puts the names in order. uniq -c counts each name. sort -r flips the counts, so the largest count comes first.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does a pipe do?