sed

Find, replace, delete, and transform text in files and streams with sed — the Linux stream editor for automated text manipulation.

sed

sed (stream editor) is a command-line tool that performs text transformations — substitutions, deletions, insertions, and line-based filtering — on input streams and files without opening an interactive editor, available on Linux, macOS, and other Unix-like systems.

What sed Does and When to Use It

sed reads input line by line, applies editing commands, and writes the result to stdout. The most common use is find-and-replace: sed 's/old/new/g' file.txt substitutes every occurrence of "old" with "new". System administrators use sed to modify configuration files in scripts, strip unwanted lines from logs, and batch-process text without manual editing.

sed processes text sequentially and does not load the entire file into memory. This makes sed efficient for processing large files — even multi-gigabyte log files. sed is not suitable for structured data manipulation (use awk for column-based work) or complex multi-line transformations (use Python or Perl).

Two major implementations exist: GNU sed (default on Linux, supports -i for in-place editing without a backup extension) and BSD sed (default on macOS, requires -i '' with an explicit empty backup extension). Commands that work on Linux may fail on macOS without adjusting the -i flag syntax. For official documentation, see man sed or gnu.org/software/sed/manual/.

How to Install sed

sed ships pre-installed on all Linux and macOS systems. On macOS, install GNU sed via Homebrew for Linux-compatible behavior:

brew install gnu-sed

GNU sed installs as gsed on macOS.

Core Concepts of sed

sed Substitution Command

The s (substitute) command is sed's most-used feature. Syntax: s/pattern/replacement/flags. The delimiter / can be replaced with any character ( s|old|new|g), which is useful when patterns contain slashes. The g flag replaces all matches on a line; without g, sed replaces only the first match.

sed In-Place Editing

The -i flag modifies the original file instead of printing to stdout. GNU sed accepts -i without arguments; BSD sed (macOS) requires -i '' (empty string) or -i .bak (create a backup). Always test sed commands without -i first.

sed Address Ranges

sed commands can target specific lines using addresses. 3d deletes line 3. 10,20s/old/new/ substitutes only on lines 10 through 20. /pattern/d deletes lines matching a regex. 1,/pattern/d deletes from line 1 through the first line matching the pattern.

Common Tasks with sed

How to Find and Replace Text in a File with sed

sed substitutes all occurrences of a string in a file and prints the result to stdout:

sed 's/old_text/new_text/g' file.txt

Apply the change directly to the file (in-place editing on Linux):

sed -i 's/old_text/new_text/g' file.txt

On macOS (BSD sed):

sed -i '' 's/old_text/new_text/g' file.txt

How to Delete Lines with sed

sed removes lines matching a pattern with the d command. Delete all blank lines:

sed '/^$/d' file.txt

Delete lines 5 through 10:

sed '5,10d' file.txt

How to Print Specific Lines with sed

sed extracts a range of lines with the -n flag and p command. Print lines 20 through 30:

sed -n '20,30p' file.txt

Print lines matching a pattern:

sed -n '/error/p' /var/log/syslog

How to Insert or Append Lines with sed

sed inserts a line before a match with i and appends after a match with a:

sed '/\[mysqld\]/a max_connections = 200' /etc/mysql/my.cnf

This appends max_connections = 200 after the line containing [mysqld].

sed Troubleshooting

Error / SymptomCauseFix
sed: -e expression #1, char X: unknown commandSyntax error — unescaped special characters or wrong delimiter→ Full article
sed: can't read file: No such file or directoryFile path is incorrect or file does not exist→ Full article
In-place edit fails on macOS: sed: 1: extra characters after \ commandBSD sed requires -i '' (empty backup extension)→ Full article
Replacement produces no changeRegex does not match; check escaping and delimiters→ Full article

grep searches for patterns but does not modify text. sed can both search and transform. See the grep article.

awk processes structured columnar data. sed handles line-level text transformations. See the awk article.