sed command
Edit text in a stream: replace, delete or insert lines.
- Text processing
- Package sed
- 8 options · 10 examples
Syntax
sed [options] 'script' [file…]Provided by the sed package. Run man sed for the full manual.
Common options
| Option | What it does |
|---|---|
-i | Edit files in place |
-i.bak | Edit in place and keep a backup |
-n | Print only lines you ask for with p |
-E | Extended regular expressions |
-e SCRIPT | Add several commands |
s/old/new/g | Replace every match on each line |
/pattern/d | Delete matching lines |
Np | Print line N (with -n) |
Examples
Replace text and print the result
sed 's/http:/https:/g' urls.txtReplace in the file itself
sed -i 's/old/new/g' config.txtReplace and keep a backup
sed -i.bak 's/8080/9090/' app.confDelete lines containing a word
sed -i '/DEBUG/d' app.logDelete blank lines
sed -i '/^$/d' file.txtDelete the first line
sed -i '1d' data.csvPrint lines 10 to 20
sed -n '10,20p' file.txtInsert a line before line 3
sed -i '3i new line' file.txtUncomment a line
sed -i 's/^#\(PermitRootLogin\)/\1/' /etc/ssh/sshd_configUse another delimiter for paths
sed 's|/var/www|/srv/www|g' site.conf
No example matches. Check the options table or search the guides.
Tips
- On macOS,
sed -ineeds an argument:sed -i '' 's/a/b/' file.
sed guides
- How to Use the Sed Command to Replace String in Files Linux Commands · Updated Apr 2025
- How to Remove Lines with Specific Text using SED Linux Commands · Updated Apr 2025
- Sed Command in Linux with 15 Practical Examples Linux Commands · Updated Apr 2025
- Sed Command to Delete Lines in a File Linux Commands · Updated Apr 2025
- Sed – Uncommenting Lines in a Text File Linux Commands · Updated Apr 2025
- How to replace newline (\n) with space using sed Linux Commands · Updated Apr 2025
- How to Replace First Line in a File using Sed Linux Commands · Updated Apr 2025
