← Lessons/Pipes & Filters
← Pipes & Filters

Count them with a pipe

Learn

The pipe |

You can now count lines (wc -l) and keep matching lines (grep). The pipe joins them. A | B means "run A, and feed everything it prints as the input to B": one command's stdout becomes the next command's stdin.

cat access.log | grep ERROR | wc -l
#   read file   →  keep ERROR lines →  count them

Data flows left to right, and the line reads almost like a sentence: take the log, keep the error lines, count them. No temporary files, no giant do-everything tool. When grep reads from a pipe it needs no filename, and neither does wc.

More filters snap in the same way: sort puts lines in order, uniq collapses adjacent duplicates. So when you hit a new problem, don't hunt for one perfect command. Ask "what small steps would get me there?" and pipe them together. That composability is the whole philosophy of the Unix command line.

Now count just the error lines: pipe grep's output into wc -l.

Interview prep

Pipelines are the Unix mindset interviewers look for.

How would you count how many times an error appears in a log?

Pipe a filter into a counter: grep ERROR app.log | wc -l. Each tool does one job; the pipe (|) feeds one's stdout into the next's stdin.

sandbox@opspath: ~
OpsPath sandbox · Debian GNU/Linux 12 · GNU bash 5.2
Each command runs in a fresh, isolated sandbox with this lesson's files in ~.
Nothing carries over between commands, so chain with && or | when you need to.
Type a command and press Enter. ↑/↓ history · Ctrl+L clear · Ctrl+C cancel line
# ── Step 3 · Count them with a pipe ──
# Sign in to run commands — it's free.