sed command (Linux / bash / sh / zsh)
What it does
Transform text streams with scripted substitutions and edits. When people search for the sed command linux examples, they usually want sed's everyday flags, safe examples, and how it differs from related tools. Below you'll find syntax, options, and copyable examples for Bash/sh/zsh.
Transform text streams with scripted substitutions and edits.
Syntax
sed [OPTION]... SCRIPT [FILE]...
Common options
| Flag / option | Meaning |
|---|---|
| -n | Suppress automatic printing. |
| -E | Enable extended regular expressions. |
| -i[SUFFIX] | Edit files in place, optionally keeping a backup. |
| -e SCRIPT | Add a script to execute. |
Practical examples
Replace text
sed 's/http:/https:/g' links.txt
Writes transformed lines to standard output.
Print a range
sed -n '10,20p' app.log
Prints only lines 10 through 20.
Edit with backup
sed -i.bak 's/debug=true/debug=false/' app.conf
Updates the file and retains a .bak copy.
Common mistakes
- Using `-i` without a backup before testing the expression.
- Forgetting that sed regular-expression syntax and `-i` behavior vary across platforms.
Related commands
- grepSearch text for lines matching a pattern.
- awkProcess record-oriented text with patterns, fields, and actions.
- trTranslate, squeeze, or delete characters in a byte stream.
- Where-ObjectFilter pipeline objects by property values or a script block predicate.
- catConcatenate files and write their contents to standard output.
- Get-ContentRead file content or provider data as a stream of objects.
- echoWrite arguments and shell-expanded values to standard output.
- echoDisplay text or control command echoing in a batch script.
FAQ
What does sed do in Linux?
Transform text streams with scripted substitutions and edits.
How do I use sed from the terminal?
Start with `sed [OPTION]... SCRIPT [FILE]...`, then choose only the options needed for the task.
What is a practical sed example?
Try `sed 's/http:/https:/g' links.txt` — Writes transformed lines to standard output.
Can I use sed in a shell script?
Yes. sed works in Bash, sh, and Zsh, but scripts should handle quoting, errors, and command output explicitly.
Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.