TExcelTAD

Pipes

A pipe (|) connects the output of one command to the input of the next. Instead of saving results to a temporary file and opening it with another tool, you build a small assembly line: list, filter, sort, count. All four shells use the same | symbol, but there is one big difference hiding behind it:

text
CMD, bash, zsh:   cmd1 --(lines of text)--> cmd2 --(lines of text)--> cmd3
PowerShell:       cmd1 --(.NET objects)---> cmd2 --(.NET objects)---> cmd3

In CMD, bash and zsh, a pipe carries plain text. The next command has to cut that text into pieces (columns, fields, words) to get at the part it wants. In PowerShell, a pipe carries objects with named properties, so the next command can ask for Length or Name directly, without parsing anything.

Windows (CMD)

CMD ships only a few filters: find, findstr, sort and more. They are enough for everyday searching and counting.

cmd · try it yourself
@echo off
(
  echo cherry
  echo apple
  echo banana
  echo apricot
) > fruit.txt
type fruit.txt | sort
echo ---
type fruit.txt | findstr "^a"
echo ---
type fruit.txt | find "an"
apple
apricot
banana
cherry
---
apple
apricot
---
banana
  • findstr understands simple regular expressions: "^a" keeps lines that begin with a, and /i makes the search case-insensitive. find searches for a plain string.
  • find /c counts matching lines instead of printing them. dir /b | find /c /v "" is the classic CMD trick to count files: /v "" selects every line that does not contain the empty string, which is all of them.
cmd
C:\Users\ada> type fruit.txt | find /c "an"
1

C:\Users\ada> dir /b | find /c /v ""
4
  • command | more shows long output one screen at a time.

Windows (PowerShell)

Each item that travels down a PowerShell pipeline is a full object. Here the files come out of Get-ChildItem as FileInfo objects, and the later commands filter, sort and pick properties by name:

powershell · try it yourself
New-Item -ItemType Directory demo | Out-Null
Set-Content demo/notes.txt ("x" * 120)
Set-Content demo/todo.txt  ("x" * 10)
Set-Content demo/log.txt   ("x" * 500)
Get-ChildItem demo |
    Where-Object Length -gt 50 |
    Sort-Object Length -Descending |
    Select-Object Name, Length
"--- first item is a: " + (Get-ChildItem demo)[0].GetType().Name

Name      Length
----      ------
log.txt      501
notes.txt    121
--- first item is a: FileInfo

Each file is one byte longer than its text because Set-Content adds a newline. Notice that nothing had to cut the size out of a line of text: Length is a property of each object. The table you see is only produced at the very end, when the objects reach the screen.

Your own data works the same way:

powershell · try it yourself
$people = @(
    [pscustomobject]@{ Name = "Ada";   Team = "core"; Commits = 42 }
    [pscustomobject]@{ Name = "Linus"; Team = "ops";  Commits = 17 }
    [pscustomobject]@{ Name = "Grace"; Team = "core"; Commits = 30 }
)
$people | Where-Object Team -eq "core" | Measure-Object Commits -Sum | Select-Object -ExpandProperty Sum
$people | Sort-Object Commits | ForEach-Object { "$($_.Name) has $($_.Commits) commits" }
72
Linus has 17 commits
Grace has 30 commits
Ada has 42 commits

Note: When a PowerShell pipeline calls a native program (like git or findstr), objects are converted to text lines on the way in and text comes back as strings. Pipes between cmdlets keep the objects.

Linux & macOS (bash/zsh)

Unix tools are designed to be chained: each one does a single job on lines of text. sort, uniq, cut, grep, wc, head and tr cover a surprising number of tasks.

bash · try it yourself
cat > access.log << 'EOF'
alice GET /home
bob GET /about
alice POST /login
carol GET /home
alice GET /home
bob GET /home
EOF
echo "Requests per user:"
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn
echo "Unique pages: $(cut -d' ' -f3 access.log | sort -u | wc -l)"
grep GET access.log | tee gets.txt | wc -l
Requests per user:
      3 alice
      2 bob
      1 carol
Unique pages: 3
5
  • cut -d' ' -f1 takes the first space-separated field: the text has to be split by hand.
  • uniq -c counts adjacent duplicates, which is why sort comes first.
  • tee copies the stream into a file and passes it on, so you can save an intermediate step.

A pipe only carries stdout. Errors still go to the screen unless you merge them first with 2>&1 | (bash also accepts |&). zsh behaves the same way here.

Text vs objects, side by side

The same question, "which files are larger than 100 bytes?", in each style:

Shell Command How it gets the size
CMD dir output is text; no size filter built in parse columns with for /f
PowerShell Get-ChildItem | Where-Object Length -gt 100 reads the Length property
bash / zsh find . -type f -size +100c or ls -l | awk '$5 > 100' parses the 5th text column

Parsing text is quick to type but fragile: a file name with a space, a different language setting or a changed column layout can break it. Objects are more verbose to type but robust.

Comparison

Task CMD PowerShell bash / zsh
Filter lines | findstr text | Where-Object / | Select-String text | grep text
Sort | sort | Sort-Object | sort
Count | find /c /v "" | Measure-Object | wc -l
First N (none) | Select-Object -First 5 | head -n 5
Unique (none) | Sort-Object -Unique | sort -u
Page through | more | Out-Host -Paging | less
Save and pass on (none) | Tee-Object f | tee f

Learn more: PowerShell Pipeline, Bash Pipes & Redirection, Batch Pipes.

Test yourself

What travels through a pipe between two PowerShell cmdlets?

  1. Lines of text
  2. Objects with properties
  3. Files
  4. Bytes only

Why does sort usually come before uniq -c in a bash pipeline?

  1. uniq only works on numbers
  2. uniq only merges duplicates that are next to each other
  3. sort removes duplicates first
  4. uniq needs a file argument

Which CMD command counts the lines that contain an in its input?

  1. findstr /b an
  2. find /c "an"
  3. sort /c an
  4. more /c an