Skip to content
shellmap

awkPattern scanning and processing language for structured text across all 5 shells

Equivalents in every shell

Bashunix
awk '{print $1}' file
Zshunix
awk '{print $1}' file
Fishunix
awk '{print $1}' file
PowerShellwindows
Get-Content file | ForEach-Object { ($_ -split '\s+')[0] }

No native awk; pipelines plus `-split` cover most one-liners.

cmd.exewindows
for /f "tokens=1" %a in (file) do @echo %a

Use `%%a` instead of `%a` inside a batch file.

Worked examples

Print the first whitespace-delimited column

Bash
awk '{print $1}' file.txt
PowerShell
Get-Content file.txt | ForEach-Object { ($_ -split '\s+')[0] }
cmd.exe
for /f "tokens=1" %a in (file.txt) do @echo %a

Sum a numeric column

Bash
awk '{s+=$2} END {print s}' file.txt
PowerShell
(Get-Content file.txt | ForEach-Object { [double](($_ -split '\s+')[1]) } | Measure-Object -Sum).Sum

Print lines where a field matches

Bash
awk '$3 == "ERROR" {print}' app.log
PowerShell
Get-Content app.log | Where-Object { ($_ -split '\s+')[2] -eq 'ERROR' }

Gotchas

  • awk fields are 1-indexed (`$1` is the first); PowerShell array indices are 0-based.
  • awk auto-splits on runs of whitespace and ignores leading/trailing whitespace; PowerShell `-split '\s+'` produces an empty leading element when the line starts with whitespace — `.Trim()` first or filter empties.
  • Multi-block awk scripts (BEGIN/END, associative arrays) rarely translate to a clean one-liner — write a small PowerShell script for non-trivial cases.

WSL & PowerShell Core notes

pwshpwsh has no `awk` cmdlet on any platform — the typical replacement is `ForEach-Object` plus `-split '\s+'` for field extraction, or `Import-Csv` for delimited structured data (the latter gives typed access by column name, not a brittle index). On Linux/macOS pwsh 7+ hosts, the system `awk` is still on `$PATH` — shell out for anything that needs `BEGIN`/`END` blocks, associative arrays, or `gensub`. Watch which implementation you get: `gawk` (default on Ubuntu/Debian/RHEL), `mawk` (some Debian minimal images), or BSD `nawk` (macOS default) — gawk extensions like `gensub`, `asorti`, and namespace blocks are NOT in mawk or BSD nawk.
WSLWSL ships GNU `gawk` on Ubuntu/Debian/Fedora distros but BusyBox `awk` on Alpine — different feature surface (BusyBox awk lacks `gensub`, `asorti`, and proper Unicode `length()`). On `/mnt/c/...` paths the per-line awk cost is negligible; the read cost dominates and is bandwidth-bound by DrvFs. For repeated awk passes over the same Windows-side dataset, copy once into `~/` (`cp /mnt/c/data/x.csv ~/x.csv`) and operate on the local ext4 copy — typical 5–10× wall-clock speedup on multi-pass pipelines.

Common tasks using awk

Related commands