← Teacher guide

Pipes

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).

The command line · Using a shell · about 45 minutes · Open the student lesson

Classroom resources

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.

Students will be able to

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

  1. Predict: students who said 6 hold Sam's misconception.
  2. Worked example: run each stage on its own, then join them.
  3. Vary: ask what head -n 2 would show without sort.
  4. 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.

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 seeA question to askThe 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  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.
  6. 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.
  7. 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.
  8. First two: Print just the first two pets in alphabetical order.

    sort pets.txt | head -n 2: cat, cat.
  9. 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.
  10. 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.
  11. 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.
  12. 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.
  13. 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.)
  14. The code: What is the code?
    2468
    GOLD 2468: you found it. Hunt complete.
  15. 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

Where it leads

Next: Who can read what (permissions).