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).
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
SlidesDownload Present from the browser, or export to PowerPoint or PDF.
WorksheetDownload 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.
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.
Try: 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.
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.
Why sort first: Why does uniq need sort before it? uniq only drops repeats that are next to each other Without sort, cat … cat aren't neighbours, so both stay.
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 projects folder and everything in it, 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.
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.