The sed command in Linux is a non-interactive stream editor that reads input line by line, applies text transformations with patterns and rules, and writes the result to standard output or back to files. Use sed for find-and-replace, line selection, deletion, insertion, and fast automation in scripts, pipelines, and server administration.
If you’re new to the sed command in Linux, this user guide walks you through everything from basic syntax to real-world one-liners used in hosting, DevOps, and sysadmin tasks. You’ll learn sed fundamentals, advanced techniques, and safe in-place editing—explained with simple examples you can run immediately.
What Is the sed Command in Linux?

sed (short for stream editor) is a powerful text-processing tool for filtering and transforming text. It’s ideal for batch modifications, automating configuration changes, cleaning logs, extracting data, and integrating into shell scripts. Unlike a text editor, sed edits streams—data flowing from files or commands—making it perfect for pipelines and non-interactive operations.
How sed Works: Stream Model and Core Syntax
Basic Syntax
sed processes input line by line, applies commands to each line that matches an address or pattern, and outputs the result. The general form:
sed [OPTIONS] 'address(es) command(s)' file(s)
# Common usage examples
sed 's/old/new/' file.txt # Replace first 'old' on each line
sed -n '1,10p' file.txt # Print only lines 1 through 10
sed '/error/d' app.log # Delete lines that match 'error'
Key options:
- -n: suppress automatic printing (use with p to control output)
- -i[SUFFIX]: edit files in-place (optionally with a backup suffix, e.g., -i.bak)
- -E: use extended regular expressions (preferred over legacy -r)
- -f script.sed: load commands from a file
- -e ‘cmd’: add multiple commands inline
Addresses and Ranges
Addresses let you target specific lines. You can use line numbers, regex patterns, or ranges:
# Line numbers
sed -n '5p' file # Print line 5
sed -n '5,12p' file # Print lines 5 through 12
# Regex addresses (delimited by /.../)
sed -n '/ERROR/p' log # Print lines containing 'ERROR'
sed '/^#/d' config # Delete commented lines
# Mixed ranges and address negation
sed '/START/,/END/p' file # Print between lines matching START and END
sed '/^$/!p' file # Print all non-empty lines (! negates the address)
Essential sed Commands with Practical Examples
Selecting and Printing Lines (p, -n, q, =)
# Print only matching lines (-n + p)
sed -n '/nginx/p' /etc/nginx/nginx.conf
# Show line numbers for matches
sed -n '/PermitRootLogin/=' /etc/ssh/sshd_config
# Stop after first match (faster on huge files)
sed '/CRITICAL/q' app.log
Find and Replace Text (s///)
The substitute command s/regex/repl/flags finds matches and replaces them. Use a different delimiter to avoid heavy escaping.
# Replace first match per line
sed 's/foo/bar/' file
# Replace all matches per line (global)
sed 's/foo/bar/g' file
# Case-insensitive replace (GNU sed)
sed 's/linux/Linux/Ig' notes.txt
# Use an alternate delimiter to avoid escaping slashes (e.g., paths)
sed 's#/var/www/html#/srv/www#' vhost.conf
# Replace only on lines that match an address
sed '/^server_name/s/example.com/example.org/' site.conf
# Replace only the 2nd occurrence per line
sed 's/foo/bar/2' input.txt
# Use captured groups and the & (whole match)
sed -E 's/([0-9]{3})-([0-9]{2})-([0-9]{4})/***-**-\3/g' data.txt
sed -E 's/error:<([^>]+)>/issue:&[\1]/' log.txt
Insert, Append, and Change Lines (i, a, c)
# Insert a line before matches
sed '/^\[mysqld\]/i # Managed by automation' my.cnf
# Append a line after matches
sed '/listen 80;/a listen 443 ssl;' nginx.conf
# Change matching lines entirely
sed '/^DocumentRoot/c DocumentRoot "/srv/www/example"' httpd.conf
Delete Lines (d)
# Remove comments and empty lines
sed -E '/^\s*($|#)/d' file
# Delete a known range
sed '10,20d' file
# Delete everything outside a block (negate the range)
sed '/BEGIN/,/END/!d' file
Multiple Edits in One Run (-e, ;, -f)
# Chain commands with -e
sed -E -e 's/\s+$//' -e 's/^\s+//' input.txt
# Or separate with semicolons
sed -E 's/\s+$//; s/^\s+//' input.txt
# Load from a script file
sed -f fixup.sed input.txt
# Example fixup.sed:
# s/localhost/127.0.0.1/g
# /^#/d
# /^\s*$/d
In-Place Editing Safely (-i) and Portability Tips
Editing files in place is convenient but risky if you skip backups. Prefer dry runs first, then apply in-place with a backup suffix.
# Dry run: preview changes
sed -E 's/http:/https:/g' site.conf | diff -u site.conf -
# Linux (GNU sed): create a backup
sed -i.bak 's/PermitRootLogin no/PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
# macOS/BSD sed: empty string means no backup
sed -i '' -E 's#<Directory /var/www>#<Directory /srv/www>#' httpd.conf
Portability notes:
- Use -E for extended regex. On older GNU sed, -r also works; on BSD/macOS prefer -E.
- Case-insensitive flag I is GNU sed. For portability, pre-normalize case or use character classes.
- macOS requires -i ” for no-backup; GNU sed accepts -i without an argument.
Advanced sed: Regex, Multi‑Line, and Hold Space
Extended Regex and Backreferences
With -E, you get modern regex features like +, ?, |, and grouping without backslashes. Backreferences let you reuse captured groups in replacements. The & symbol expands to the whole match.
# Normalize repeated whitespace to a single space
sed -E 's/[[:space:]]+/ /g' file
# Swap first and last name
sed -E 's/^([A-Za-z]+)\s+([A-Za-z]+)$/\2 \1/' names.txt
# Keep only digits from each line
sed -E 's/[^0-9]+//g' data.txt
Multi-Line Editing (N) and Line Joins
By default sed works per line. Use N to append the next line to the pattern space and operate across line boundaries.
# Join pairs of lines with a space
sed 'N; s/\n/ /' file
# Remove blank lines that follow another blank line (collapse multiple blanks)
sed -E ':a; /^\s*$/N; s/^\s*\n+/\n/; ta' file
# Replace a token that may be split across two lines
sed 'N; s/TOKEN\nCONTINUED/TOKEN CONTINUED/; P; D' file
Hold Space Tricks (h, H, g, G, x)
sed has two buffers: pattern space (current line) and hold space (scratchpad). Move data between them for advanced logic.
# Print each line along with the previous line
sed '1!G; h; $!p' file
# Swap current and previous lines
sed 'x; 1!p; h' file
Branching and Conditional Replacements (b, t)
Branch to labels to build conditional flows. The t command branches only if a successful substitution occurred.
# If we replaced 'alpha', also replace 'beta' on the same line
sed -E ':start; s/alpha/A/g; t next; b end; :next s/beta/B/g; :end' file
Real-World sed One-Liners for Sysadmins and DevOps
These practical examples solve common tasks on servers and in CI/CD pipelines.
1) Strip comments and trailing spaces from configs:
sed -E 's/\s+$//; /^\s*($|#)/d' config.conf
2) Bulk HTTPS upgrade in Nginx vhosts:
find /etc/nginx/sites-available -type f -name '*.conf' -print0 \
| xargs -0 sed -i.bak 's/http:/https:/g'
3) Rotate logs by keeping the last 1000 lines:
sed -n -e :a -e '$q;N;101,$D;ba' app.log > app.log.tail
4) Mask email usernames for privacy in logs:
sed -E 's/([A-Za-z0-9._%+-])[A-Za-z0-9._%+-]*(@[A-Za-z0-9.-]+\.[A-Za-z]{2,})/\1***\2/g' access.log
5) Convert CSV delimiter from comma to tab (not inside quotes – keep it simple):
sed 's/,/\t/g' data.csv > data.tsv
6) Update a key’s value only within a specific block:
sed -E '/<VirtualHost \*:80>/, /<\/VirtualHost>/ s/ServerName .*/ServerName example.org/' vhost.conf
7) Remove duplicate adjacent lines (simple dedupe):
sed '$!N; /^\(.*\)\n\1$/!P; D' file
sed vs awk vs grep: When to Use Which
- Use grep to filter lines by pattern. It’s fast, simple, and best for pure matching.
- Use sed to transform text streams: find/replace, delete/insert lines, small logic, and quick one-liners.
- Use awk for column-aware processing, calculations, and structured outputs (think CSV/TSV reports).
In many pipelines, you combine them: grep narrows data, sed cleans/formats, awk aggregates or computes.
Performance and Best Practices
- Test with -n and p before in-place edits. Pair with diff for confidence.
- Quote commands with single quotes to prevent shell expansion: ‘s/$HOME/…/’ not “s/$HOME/…/”.
- Choose smart delimiters (| # ~) to avoid escaping slashes in paths and URLs.
- Anchor patterns with ^ and $ to reduce false positives and speed up matching.
- Prefer -E for clearer regex syntax. Keep expressions readable.
- For large jobs, store rules in a .sed file (-f). This improves maintainability and speed.
- Set LC_ALL=C for faster byte-wise processing when Unicode rules aren’t required.
- Beware of portability: GNU vs BSD differences for -i and regex flags.
Practice Safely on a Staging Server
When you batch-edit configs or logs with sed, test on a staging environment first. With a YouStable VPS, you get full root access, snapshot backups, and SSH out of the box—so you can trial sed one-liners, roll back instantly, and push changes to production with confidence. It’s a simple way to avoid risky edits on live servers.
Sed Cheat Sheet: Commands You’ll Use Daily
- Print: -n ‘addr p’ file
- Find & replace: ‘s/old/new/g’
- Delete: ‘addr d’
- Insert/Append: ‘addr i text’ / ‘addr a text’
- Change line: ‘addr c text’
- Line numbers: ‘=’
- Multiple commands: -e ‘cmd1’ -e ‘cmd2’ or ‘cmd1; cmd2’
- Extended regex: -E
- In-place edit: -i[.bak]
Common Pitfalls and How to Avoid Them
- Accidental global changes: Start with a narrow address, then widen scope.
- Quoting issues: Use single quotes; escape only what sed needs.
- Overwriting without backup: Always use -i.bak or version control.
- Regex greediness: Use anchors and character classes to match precisely.
- Platform mismatch: Adjust -i and regex flags on macOS vs GNU/Linux.
FAQs: sed Command in Linux
What is the sed command used for in Linux?
sed is a stream editor that automates text transformations such as search-and-replace, deleting or inserting lines, and formatting. It’s widely used in shell scripts, CI/CD pipelines, log cleanup, and configuration management because it’s fast, scriptable, and works line by line without opening an editor.
How do I replace text in all files recursively with sed?
Combine find with sed -i for safe, recursive edits:
find . -type f -name ‘*.conf’ -print0 \
| xargs -0 sed -i.bak ‘s/old.domain/new.domain/g’
This backs up each file with .bak. Inspect changes with git diff or diff before removing backups.
What’s the difference between GNU sed and BSD/macOS sed?
Key differences include in-place editing syntax (-i ” on macOS vs -i on GNU) and some flags (GNU supports I for case-insensitive s///). Extended regex is -E on BSD/macOS and also supported on modern GNU sed. Test portability when writing cross-platform scripts.
How can I edit a file in place without creating a backup?
On GNU/Linux, use -i without a suffix. On macOS/BSD, pass an empty string:
# GNU/Linux
sed -i ‘s/http:/https:/g’ file
# macOS/BSD
sed -i ” ‘s/http:/https:/g’ file
How do I use extended regex with sed?
Use -E to enable extended regular expressions so you can write cleaner patterns without excessive escaping:
sed -E ‘s/([A-Za-z]+)\s+([A-Za-z]+)/\2 \1/’ names.txt
How do I replace only the nth occurrence on a line?
Add the occurrence number as a flag to the s command. This example replaces only the second instance per line:
sed ‘s/error/warning/2’ log.txt
Is sed faster than awk or Python for simple replacements?
For basic find-and-replace or line filtering, sed is typically faster and uses less memory than awk or Python due to minimal startup overhead and streaming execution. For structured parsing or computations, awk/Python may be more suitable despite a performance tradeoff.
Final Word
With these techniques and best practices, you can confidently use the sed command in Linux to automate text processing, refactor configs, and streamline server workflows. If you want a safe playground with snapshots and full SSH, consider testing on a YouStable VPS before rolling changes into production.