Skip to content
AgileLogics
5 min read

Pipes: Connecting Commands

Prerequisites: Redirection: sending output to files

A pipe (|) sends one command's output straight into the next command's input. Pipes let you build powerful tools from small, simple programs — the heart of the Unix philosophy.

Real-world analogy: an assembly line. Each station does one job and passes its work to the next.

printf '%s\n' banana apple cherry apple banana apple > fruits.txt

cat fruits.txt | sort
echo "---"
sort fruits.txt | uniq
echo "---"
sort fruits.txt | uniq -c | sort -rn
apple
apple
apple
banana
banana
cherry
---
apple
banana
cherry
---
      3 apple
      2 banana
      1 cherry

Reading the last pipeline left to right:

  1. sort fruits.txt — sort the lines, so identical ones are next to each other.
  2. uniq -c — collapse repeated lines, prefixing each with its count.
  3. sort -rn — sort numerically (-n), largest first (-r).

The result: a ranking of the most common lines. This "sort, count, sort" pattern works on anything — IP addresses in a log, words in a document, errors by type.

Filtering with grep

grep keeps only the lines that match a pattern:

printf '%s\n' "INFO start" "ERROR disk full" "INFO retry" "ERROR timeout" > app.log

grep "ERROR" app.log
grep -c "ERROR" app.log
grep -v "ERROR" app.log | wc -l
ERROR disk full
ERROR timeout
2
2
  • grep -c counts matching lines.
  • grep -v keeps lines that don't match.

More pipe-friendly tools

echo "hello world" | tr 'a-z' 'A-Z'
echo "one,two,three" | cut -d, -f2
seq 1 5 | awk '{ total += $1 } END { print total }'
HELLO WORLD
two
15
  • tr translates characters.
  • cut -d, -f2 takes the second comma-separated field.
  • seq 1 5 prints 1 to 5; awk adds up the numbers. (awk is a small language for processing columns of text — Level 2 covers it.)

Common mistakes

  • "Useless use of cat": cat file | grep x works, but grep x file is simpler. Using cat at the start is fine when it makes a pipeline clearer.
  • Forgetting to sort before uniq — uniq only merges adjacent duplicate lines.

Practice

Given a file of words, one per line, print how many different words it contains.

Show solution
printf '%s\n' red blue red green blue red > words.txt
sort words.txt | uniq | wc -l
3