Bash sed Command: Find, Replace, and Edit Files From the Terminal
March 17, 2026
The bash sed command (stream editor) processes text line by line — finding patterns, replacing content, deleting lines, and inserting text — making it indispensable for modifying config files, cleaning log output, or batch-editing text. The learning curve feels steep at first because sed's syntax is dense, but a handful of patterns cover 90% of real-world use cases. This tutorial walks through every major sed operation with practical examples.
1. Basic Find and Replace
# Basic: replace first occurrence per line
sed 's/old/new/' file.txt
# /g flag: replace ALL occurrences per line
sed 's/old/new/g' file.txt
# /i flag: case-insensitive replace (GNU sed only)
sed 's/error/WARNING/gi' file.txt
# Combine flags
sed 's/old/new/gI' file.txt # global + case-insensitive
# Use different delimiter (helpful when pattern contains /)
sed 's|/usr/local|/opt|g' paths.txt
# Preview output (doesn't modify file)
sed 's/old/new/g' file.txt
2. In-Place Editing with -i
To modify a file directly instead of printing to stdout, use -i:
# Modify file in-place (Linux/GNU sed)
sed -i 's/old/new/g' file.txt
# macOS requires an extension argument (use empty string for no backup)
sed -i '' 's/old/new/g' file.txt
# Create a backup before editing (adds .bak suffix)
sed -i.bak 's/old/new/g' file.txt
# Creates file.txt.bak (original) and modifies file.txt
# Replace in multiple files
sed -i 's/old/new/g' *.conf
sed -i 's/localhost/192.168.1.10/g' config/*.yaml
3. Deleting Lines
# Delete lines matching a pattern
sed '/^#/d' config.txt # delete comment lines (start with #)
sed '/^$/d' file.txt # delete blank lines
sed '/error/d' app.log # delete lines containing "error"
# Delete by line number
sed '5d' file.txt # delete line 5
sed '3,7d' file.txt # delete lines 3 through 7
sed '$d' file.txt # delete last line
# Delete from pattern to end of file
sed '/^START/,/^END/d' file.txt # delete everything between START and END lines
4. Print Specific Lines
# -n suppresses automatic printing; p prints matching lines
sed -n '5p' file.txt # print only line 5
sed -n '3,7p' file.txt # print lines 3 to 7
sed -n '/ERROR/p' app.log # print only lines containing ERROR
# Print lines NOT matching (like grep -v)
sed -n '/^#/!p' config.txt # print non-comment lines
5. Insert and Append Lines
# Append line AFTER matching line (a command)
sed '/^HOST=/a PORT=5432' config.txt
# Insert line BEFORE matching line (i command)
sed '/^[Service]/i [Unit]' systemd.conf
# Replace entire matching line (c command)
sed '/^db_host=/c db_host=newserver.example.com' config.ini
6. Multiple Commands with -e
# Run multiple sed expressions in one pass
sed -e 's/foo/bar/g' -e 's/baz/qux/g' -e '/^#/d' file.txt
# Equivalent using semicolons
sed 's/foo/bar/g; s/baz/qux/g; /^#/d' file.txt
# Practical: clean a config file
sed -i \
-e 's/localhost/10.0.0.5/g' \
-e '/^#/d' \
-e '/^$/d' \
app.conf
7. Practical Examples
# Add a prefix to every line
sed 's/^/PREFIX: /' file.txt
# Remove trailing whitespace from all lines
sed -i 's/[[:space:]]*$//' file.txt
# Remove HTML tags
sed 's/<[^>]*>//g' page.html
# Strip ANSI color codes from terminal output
sed 's/\x1b\[[0-9;]*m//g' colored-output.txt
# Comment out a specific line in a config
sed -i '/^PasswordAuthentication yes/s/^/#/' /etc/ssh/sshd_config
# Replace only in lines matching a pattern (address + command)
sed '/^server_name/s/example.com/newdomain.com/' nginx.conf
# Batch rename: add prefix to all filenames listed in a file
cat filelist.txt | sed 's/^/mv /' | bash # generates mv commands
For text searching before editing, combine sed with the bash grep tutorial. For multi-line transformations and structured data, awk is often cleaner than sed. For simple string operations within a Bash script without forking a process, see bash string manipulation.
Summary
The core sed syntax is sed 's/pattern/replacement/flags'. Add /g for global replace, -i for in-place editing, and /d to delete matching lines. Use -n with p to print specific lines. Combine multiple operations with -e or semicolons. When editing files, always either create a backup with -i.bak or preview the change first without -i.