← Teacher guide

Pipes

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

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

Classroom resources

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.

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.

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

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