🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

sed Command

Intermediate Text Processing man(1)

Stream editor for filtering and transforming text

👁 9 views 📅 Updated: Mar 15, 2026
SYNTAX
sed [OPTION]... 'SCRIPT' [FILE]...

What Does sed Do?

sed (stream editor) is a powerful non-interactive text editor that processes text line by line. It is primarily used for find-and-replace operations, text transformations, and line-level editing in files and pipelines.

sed reads input, applies editing commands, and writes the result to standard output — the original file is unchanged unless -i (in-place) is used. This makes sed safe for testing transformations before applying them. It supports basic and extended regular expressions for complex pattern matching.

Common uses include substituting text, deleting lines, inserting content, and transforming configuration files. sed is particularly valuable in shell scripts and automation pipelines where interactive editing is not possible.

Options & Flags

OptionDescriptionExample
-i Edit files in place (modify the original file) sed -i 's/old/new/g' file.txt
-i.bak Edit in place with backup sed -i.bak 's/old/new/g' file.txt
-e Add multiple editing commands sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt
-n Suppress automatic output (use with p command) sed -n '5,10p' file.txt
-E (-r) Use extended regular expressions sed -E 's/([0-9]+)/[\1]/g' file.txt
s///g Substitute command with global flag (replace all) sed 's/http/https/g' urls.txt
d Delete matching lines sed '/^#/d' config.conf

Practical Examples

#1 Simple find and replace

Replaces all occurrences of foo with bar. Output goes to stdout.
$ sed 's/foo/bar/g' input.txt

#2 In-place edit with backup

Modifies the file directly, saving the original as config.yml.bak.
$ sed -i.bak 's/localhost/192.168.1.100/g' config.yml

#3 Delete comment lines

Removes all lines starting with # (comments) from the output.
$ sed '/^#/d' /etc/ssh/sshd_config

#4 Delete empty lines

Removes all blank lines from the output.
$ sed '/^$/d' file.txt

#5 Print specific line range

Prints only lines 10 through 20 from the file.
$ sed -n '10,20p' logfile.txt

#6 Insert text before a line

Inserts a comment line before every line starting with "server".
$ sed '/^server/i # Added by admin' nginx.conf

#7 Replace with regex capture groups

Converts YYYY-MM-DD dates to DD/MM/YYYY format using capture groups.
$ sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3\/\2\/\1/g' dates.txt

#8 Multiple replacements

Applies two separate substitution commands to the same file.
$ sed -e 's/old/new/g' -e 's/wrong/right/g' document.txt

Tips & Best Practices

Always test before -i: Run sed without -i first to preview changes in stdout. Only add -i when confident the result is correct, or use -i.bak for safety.
Use different delimiters: When patterns contain slashes (like file paths), use a different delimiter: sed 's|/var/www|/srv/http|g' avoids escaping slashes.
sed vs perl -pe: For complex regex with lookaheads/lookbehinds not supported in sed, use perl -pe 's/pattern/replacement/g' instead.

Frequently Asked Questions

How do I replace text in a file permanently?
Use sed -i 's/old/new/g' filename. The -i flag edits the file in place. Add .bak after -i to create a backup: sed -i.bak 's/old/new/g' filename.
What does the g flag mean in sed?
Without g, sed only replaces the first match on each line. With g (global), it replaces ALL matches on each line: sed 's/a/b/g' replaces every 'a' with 'b'.
How do I delete a specific line number?
Use sed 'Nd' where N is the line number: sed '5d' file.txt deletes line 5. For a range: sed '5,10d' deletes lines 5 through 10.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →