A pipe (|) sends one command's output into the next command instead of the screen, so small commands combine: grep finds lines, wc -l counts them, sort puts them in order, uniq drops repeats, head keeps the first few, and > saves the result.
No command counts how many different pets are in a list. Three tiny commands joined together do.
Each command does one small job. A pipe, the | character, joins them: the output of the command on the left becomes the input of the command on the right.
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Try: Try it on the board and compare the result with your prediction.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Practise: A few on your own, to check you have it.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
What comes out?
TryTry it on the board and compare the result with your prediction.
Try: count with a pipe
Was your prediction right?
Try it yourselfYour edits are saved in this browser.
Run grep cat pets.txt, then the same with | wc -l on the end.
Use the board. This ticks when you get there.
InvestigateWork out why it happens, one step at a time.
Worked example: a three-part pipe
Worked exampleBefore each step, say what you think comes next.
sort pets.txtcat, cat, catfish, dog, hamster, parrot: same lines, in order.
one line, three jobsEach command is tiny; the pipe joins them into something new.
Now count the lines after uniq.
pets.txt is cat, dog, catfish, parrot, cat, hamster. How many lines does sort pets.txt | uniq print?
Sam explains a pipe. Click the line where it first goes wrong.
ModifyChange the problem on purpose and see what happens.
Vary: different pipes
Try it yourselfYour edits are saved in this browser.
How many different pets are there? Use sort, uniq and wc -l in one line.
Use the board. This ticks when you get there.
Print just the first two pets in alphabetical order.
Use the board. This ticks when you get there.
Save the lines with cat in them into a new file called cats.txt.
Use the board. This ticks when you get there.
PractiseA few on your own, to check you have it.
Build the pipe
MakeMake your own problem using what you have learned. Expect a few tries; that is normal.
The treasure hunt, part 2
Then: think of a question about a long list (your playlist, a class list) that a pipe could answer in one line.
Try it yourselfYour edits are saved in this browser.
You're in the office. The clue said: the right code is the only line with the word GOLD in it. Print just that line.
Use the board. This ticks when you get there.
What is the code?
One more: how many silver codes are there? Use a pipe.
Use the board. This ticks when you get there.
For teachers
Students join commands with pipes (grep, wc, sort, uniq, head), save output with >, and finish the treasure hunt by finding an exact line and counting with a pipe (AC9TDI10K01 node 1). About 45 minutes.
Try the boards yourself; the Make finishes the treasure hunt (office/codes.txt; the answer is 2468, the only line with GOLD in capitals).
The one idea: a pipe sends one command's output into the next, so small tools combine. This is how people answer questions about huge logs on servers.
Running the lesson
Predict: students who said 6 hold Sam's misconception.
Worked example: run each stage on its own, then join them.
Vary: ask what head -n 2 would show without sort.
Make: ask why grep -i GOLD gives two lines.
The hard part (on purpose)
Students try to do everything with one command. Ask them to say each stage aloud: "find the lines, then count them".
When students get stuck
A student thinks wc reads the file (Sam's mistake). Ask: Run just the part before the |. What does wc receive? Fix: Each command after a pipe reads the previous output.
uniq leaves repeats. Ask: Are the repeats next to each other? Fix: sort first.
Stretch and support
Stretch: Which pet name is the longest? (No single command does it; think about what you'd need.)
Support: A pipe diagram: file → grep → wc → screen, with the data written on each arrow.
Where it leads
Next: Who can read what (permissions).
What each step is for
what comes out?: Committing to the output of a two-command pipe tests the new move: the second command reads the first one's output.
Try: count with a pipe: Running the pipe settles the prediction.
Worked example: a three-part pipe: Following data through sort, uniq and wc models the new move one stage at a time. Sam thinks wc still reads the file, the misconception planted after the method.
Vary: different pipes: Each task changes one stage of the pipe: count different pets, keep only the first two in order, then save a result into a file with >.
build the pipe: Choosing pipes for new questions is the same move in mixed forms, plus a review of changing files.
the treasure hunt, part 2: Finishing the hunt with grep and a pipe puts the course together: find the exact line, read the code, then count with a pipe.