Students join commands with pipes (grep, wc, sort, uniq, head), see that the operating system connects one program's output to the next one's input (so neither needs to know about the other), save output with >, and finish the treasure hunt by finding an exact line and counting with a pipe (AC9TDI10K01 node 1).
Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
WorksheetOpen in PrintDownload Print the student copy; the Teacher edition has the answers.
Students will be able to
I can join commands with a pipe.
I can count, sort and remove repeats.
I can save a command's output into a file.
Timing
Predict and Try 6 min · Worked example 12 min · Vary 12 min · Practise and Make 15 min
Before class
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.
In class
On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.
Think, pair, share(start) No command counts how many different pets are in a list. How could small commands do it together?
What if one command's output became the next command's input?
In what order would you put them?
Let students order sort, uniq and wc before the board. Revisit at the end.
Pass it on(predict, 5 min)
Three students stand in a row as sort, uniq and wc. A list on a card goes in at one end: sort puts it in order, uniq drops neighbouring repeats, wc counts the lines.
Sort groups repeats, uniq drops them, wc counts: the order matters.
Swap the order(modify, 3 min)
Run it again with uniq before sort. What goes wrong?
Repeats aren't neighbours, so uniq misses them. This is why sort comes first.
How many lines?(investigate) pets.txt is cat, dog, cat, bird. How many lines does sort pets.txt | uniq | wc -l print? 3 After sort the two cats are together; uniq keeps one: bird, cat, dog.
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
What you will see
A question to ask
The fix
A student thinks wc reads the file (Sam's mistake).
Run just the part before the |. What does wc receive?
Each command after a pipe reads the previous output.
uniq leaves repeats.
Are the repeats next to each other?
sort first.
Answers to the checks
what comes out?: grep cat pets.txt prints cat, catfish and cat. What does grep cat pets.txt | wc -l print? 3. The three lines go into wc -l instead of the screen, and wc -l counts them.
Count with a pipe: Run grep cat pets.txt, then the same with | wc -l on the end.
3: wc counted grep's lines, not the file's.
Keep the first lines: One more small command: head keeps the first few lines. Run head -n 2 pets.txt.
cat and dog: the first two lines of the file, in the order they're stored. head -n 3 would keep three.
sort pets.txt | uniq | wc -l: pets.txt is cat, dog, catfish, parrot, cat, hamster. How many lines does sort pets.txt | uniq print? 5 sort puts the two cats next to each other; uniq keeps one. cat, catfish, dog, hamster, parrot: 5.
Find the mistake: Sam explains a pipe. Click the line where it first goes wrong. line 3 Line 3: after the |, wc -l reads grep's output (three lines), not pets.txt. The answer is 3.
Who joins them?: wc -l can count the lines of a file, lines you type, or grep's output. How does wc know which one it's getting? It doesn't need to: the operating system hands it lines, and it counts them Each program reads its input and writes its output. The operating system decides where those go: the screen, a file, or the next program. That's why tiny programs combine.
Different pets: How many different pets are there? Use sort, uniq and wc -l in one line.
sort pets.txt | uniq | wc -l prints 5.
First two: Print just the first two pets in alphabetical order.
sort pets.txt | head -n 2: cat, cat.
Save it: Save the lines with cat in them into a new file called cats.txt.
grep cat pets.txt > cats.txt: the output goes into the file instead of the screen.
Count visits: A server log has a line for every visit. Which pipe counts the visits from Australia (lines with AU)? grep AU log.txt | wc -l Find the lines first, then count them.
First name: What does sort names.txt | head -n 1 print? The name that comes first alphabetically sort orders the names; head keeps the first.
Review: rm: Review: what does rm -r projects do? Deletes the folder and all inside, for good -r means the folder and everything inside it.
The GOLD line: 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.
grep GOLD codes.txt. (grep -i GOLD would also catch "gold 5530": capitals matter here.)
The code: What is the code? 2468 GOLD 2468: you found it. Hunt complete.
Count with a pipe: One more: how many silver codes are there? Use a pipe.
grep silver codes.txt | wc -l prints 2.
Solutions
One possible solution for each task. Other correct programs are fine.
Vary: different pipes › Different pets
Open task: any program that does what the step asks.
Vary: different pipes › First two
Open task: any program that does what the step asks.
Vary: different pipes › Save it
Open task: any program that does what the step asks.
the treasure hunt, part 2 › The GOLD line
Open task: any program that does what the step asks.
the treasure hunt, part 2 › The code
Open task: any program that does what the step asks.
the treasure hunt, part 2 › Count with a pipe
Open task: any program that does what the step asks.
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.
Exit ticket
Write one line that counts how many lines of pets.txt contain the letter o.
grep o pets.txt | wc -l (or grep -c o pets.txt).
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. The last question is the node's idea (10K01 n1): the operating system joins the programs, so neither needs to know where its input comes from.
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.