GREPWISE

Pipes explained: Lego bricks for commands

A pipe sends stdout from one command into stdin of the next. Filter, reshape, sort, and count without writing a temporary file.

Pipes explained: Lego bricks for commands

HOOK - 0-3s

A pipe is how small commands stop being small.

PROBLEM - 3-10s

New shell users often treat commands as isolated tools. Then every second step becomes copy, paste, or a temporary file.

SOLUTION - 10-40s

Let one command print exactly what the next command wants.

printf 'prod api up\nstage api down\nprod web up\nprod api up\n' |
  grep '^prod' |                                                  # keep prod lines
  awk '{print $2}' |                                              # keep service name
  sort |
  uniq -c                                                         # count repeats

printf 'nginx:80\nssh:22\nnginx:443\n' |
  cut -d: -f1 |
  sort |
  uniq                                                            # reduce to unique service names
   2 api
   1 web

nginx
ssh

PAYOFF - 40-50s

The payoff is composition. Each command stays dumb and narrow. The pipeline does the thinking.

CTA - 50-55s

When you want a temp file, try a pipe first.

exit 0

#pipes#bash#linux#cli

❯ exit 0

cd ..