Skip to content
shellmap

sedStream editor for filtering and transforming text across all 5 shells

Equivalents in every shell

Bashunix
sed 's/old/new/g' file
Zshunix
sed 's/old/new/g' file
Fishunix
sed 's/old/new/g' file
PowerShellwindows
(Get-Content file) -replace 'old', 'new'

No native sed. `-replace` is regex-based and works on in-memory content.

cmd.exewindows
powershell -Command "(Get-Content file) -replace 'old','new'"

cmd has no sed equivalent — shell out to PowerShell or install GNU sed.

Worked examples

Replace text in a file in place

Bash
sed -i 's/foo/bar/g' file.txt
PowerShell
(Get-Content file.txt) -replace 'foo','bar' | Set-Content file.txt
cmd.exe
powershell -Command "(Get-Content file.txt) -replace 'foo','bar' | Set-Content file.txt"

Delete lines matching a pattern

Bash
sed '/^#/d' file.txt
PowerShell
Get-Content file.txt | Where-Object { $_ -notmatch '^#' }

Print only lines matching a regex

Bash
sed -n '/error/p' app.log
PowerShell
Select-String -Path app.log -Pattern 'error' | ForEach-Object Line

Gotchas

  • macOS/BSD `sed -i` requires a backup suffix: `sed -i '' 's/.../.../' file`. GNU sed accepts `-i` alone.
  • PowerShell `-replace` is regex by default — escape special characters or pre-process with `[regex]::Escape()`.
  • Piping `Get-Content file | Set-Content file` in PowerShell will truncate the file mid-read; assign to a variable or use `-Raw` plus a temp file.

WSL & PowerShell Core notes

pwshpwsh has no `sed` cmdlet. The closest in-place edit is `(Get-Content file -Raw) -replace pattern,replacement | Set-Content file -NoNewline` — but the `-replace` operator is regex (not POSIX BRE), so character classes and capture-group refs (`$1` vs `\1`) differ. On Linux/macOS pwsh, the system `/usr/bin/sed` is still available — invoke it directly when you need POSIX semantics.
WSLWSL ships GNU sed, which accepts `sed -i 's/.../.../' file` (no backup suffix needed). Inside WSL, `sed -i 's/\r$//' file` is the canonical CRLF stripper for Windows-authored scripts before running them under bash. On `/mnt/c/...` paths the edit works but DrvFs makes it slower than the same edit on a `~/` path; for build pipelines, copy in, sed, copy out.

Common tasks using sed

Related commands