sed — Stream editor for filtering and transforming text across all 5 shells
Equivalents in every shell
sed 's/old/new/g' filesed 's/old/new/g' filesed 's/old/new/g' file(Get-Content file) -replace 'old', 'new'No native sed. `-replace` is regex-based and works on in-memory content.
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
sed -i 's/foo/bar/g' file.txt(Get-Content file.txt) -replace 'foo','bar' | Set-Content file.txtpowershell -Command "(Get-Content file.txt) -replace 'foo','bar' | Set-Content file.txt"Delete lines matching a pattern
sed '/^#/d' file.txtGet-Content file.txt | Where-Object { $_ -notmatch '^#' }Print only lines matching a regex
sed -n '/error/p' app.logSelect-String -Path app.log -Pattern 'error' | ForEach-Object LineGotchas
- 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
Common tasks using sed
- Convert line endings between CRLF and LF
Strip or insert carriage returns to switch a file between Windows (CRLF, \r\n) and Unix (LF, \n) line endings.
- Extract a substring by regex
Pull a substring out of a string or a stream of input using a regex — for parsing log lines, extracting an ID from a URL, scraping a version number, or tokenizing a config.
- Find and replace text in files
Substitute one string for another inside a file (or every file in a tree), in place.
- Normalize whitespace in text
Collapse multiple spaces / tabs into single spaces and strip line-leading / trailing whitespace — for diff-friendly text, CSV cleanup, and config-file canonicalization.
- Replace a string across multiple files
Apply the same find-and-replace operation to a batch of files (filtered by glob, by content, or by recursion) — for renaming an API, fixing a typo across a codebase, or updating a config value.
- Strip ANSI color codes from output
Remove ESC[…m terminal color sequences from text — for clean log archives, machine-parseable output, and pasting to non-color-aware destinations.
- Trim leading and trailing whitespace
Remove only the whitespace at the start and end of each line (preserving internal spaces) — for cleaning user input, config-file values, and form fields.