Pipe command output to grep
Filter the stdout of one command through a pattern matcher and print only matching lines.
How to pipe command output to grep in each shell
Bashunix
ls -la | grep "\.log$"`grep` reads stdin line-by-line and prints matches. Add `-i` for case-insensitive, `-v` to invert, `-E` for extended regex.
Zshunix
ls -la | grep "\.log$"Fishunix
ls -la | grep "\.log$"PowerShellwindows
Get-ChildItem | Select-String "\.log$"`Select-String` matches against the **stringified projection** of each piped object. For pure text filtering use `... | Where-Object { $_ -match "\.log$" }`.
cmd.exewindows
dir | findstr "\.log$"`findstr` regex is a limited dialect (no `+`, no `{n,m}`, no lookarounds). `/R` enables it; without `/R` the pattern is treated as a substring search.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- PowerShell pipes carry **objects**, not text. `Get-Process | grep firefox` works only because each Process object is converted to its string projection — brittle in scripts.
- Idiomatic PowerShell: `Get-Process | Where-Object Name -match 'firefox'`. Idiomatic Unix: `ps aux | grep firefox`. They look similar but operate on different data shapes.
- `findstr` does not support PCRE alternation; `findstr /R "error|warn"` is a literal `|`, not OR. Use multiple `/C:` flags: `findstr /C:error /C:warn`.
- On Windows, prefer `Select-String` over `findstr` whenever you need real regex — `Select-String` uses .NET regex (full PCRE-style support).