sed
The Unix stream editor that pioneered non-interactive, line-by-line text transformation and became a cornerstone of shell scripting
Created by Lee E. McMahon
sed - short for stream editor - is a Unix utility and tiny domain-specific language for parsing and transforming text. Rather than opening a file interactively, sed reads input line by line, applies a script of editing commands to each line, and writes the result to standard output. This non-interactive, pipeline-friendly design made sed one of the foundational tools of the Unix philosophy and a fixture of shell scripting to this day.
History & Origins
sed was written by Lee E. McMahon at AT&T Bell Labs, developed from roughly 1973 to 1974. It was distributed widely with Version 7 Unix in 1979, which carried it to the broader Unix community.
From ed to a Stream Editor
sed descends directly from ed, the original Unix line editor (1971), which itself traced back to the earlier qed editor (mid-1960s). McMahon’s insight was that many text-processing chores did not need an interactive editor at all - they needed an editor that could run unattended as part of a pipeline.
Before sed, Bell Labs programmers wrote small special-purpose programs for recurring jobs, including a tool called gres (a play on the ed command g/re/s - globally apply to lines matching a regular expression a substitution). Rather than keep writing one-off utilities, McMahon generalized the idea into a single configurable stream editor. The result reused ed’s familiar command syntax and regular-expression engine but applied them automatically to a stream of input.
Spreading Through Unix
Because sed shipped as a standard Unix utility, it became ubiquitous. Its commands and regular-expression style fed directly into later text-processing tools. AWK (1977) extended the pattern-action idea, and Perl (1987) was explicitly conceived by Larry Wall as a fusion of sed, AWK, and the shell - one reason sed’s substitution syntax feels so familiar to Perl programmers.
Design Philosophy
sed embodies the Unix philosophy: do one thing well, work as a filter, and compose with other tools through pipes.
- Non-interactive - sed runs a fixed script against its input with no human in the loop, making it ideal for automation.
- Stream-oriented - input flows through one line at a time, so sed can process arbitrarily large files (or endless streams) with a small, constant memory footprint.
- Declarative-leaning - a sed program is largely a set of address/command rules (“on lines matching this pattern, do that”), though it also supports branching and looping that make it surprisingly capable.
- Composable - sed is designed to sit in the middle of a pipeline between tools like
grep,sort, andawk.
How sed Works
sed processes input through a simple but powerful cycle:
- Read one line of input into a buffer called the pattern space (the trailing newline is stripped).
- Apply each command in the script, in order, to the pattern space - subject to optional addresses that restrict which lines a command affects.
- Unless auto-print is suppressed (
-n), print the pattern space. - Repeat for the next line.
A second buffer, the hold space, persists between cycles. Commands can exchange or append data between the pattern space and hold space, which is what lets sed do multi-line operations like reversing a file (tac), joining lines, or implementing small state machines. These features make sed Turing-complete, even though most real-world scripts are a single substitution.
Addresses
Commands can be limited to specific lines:
| |
Key Features
Substitution
The s command is sed’s workhorse and by far its most common use:
| |
The & metacharacter refers to the whole match, and \1, \2, … refer to captured groups:
| |
Core Commands
| Command | Action |
|---|---|
s/re/repl/ | Substitute text matching a regular expression |
p | Print the pattern space |
d | Delete the pattern space and start the next cycle |
a, i, c | Append, insert, or change lines of text |
y/abc/xyz/ | Transliterate characters (like tr) |
n, N | Read the next line into (or append it to) the pattern space |
h, H, g, G, x | Move data between pattern and hold spaces |
b, t, :label | Branch, conditional branch, and labels for loops |
q | Quit |
Common One-Liners
| |
Evolution
The original Bell Labs sed was refined and re-implemented many times across Unix variants. Three lineages matter most today:
POSIX sed
sed’s behavior was standardized by POSIX (IEEE Std 1003.2-1992) and carried into the Single UNIX Specification, defining a portable core of commands and basic regular expressions that scripts can rely on across systems.
GNU sed
The GNU implementation, maintained by the Free Software Foundation, is the version found on most Linux distributions. It adds widely used extensions including:
-ifor in-place editing of files-E/-rfor extended regular expressions-zfor NUL-separated (“null data”) records- Convenience escapes such as
\n,\t,\L,\U, and\w
GNU sed 4.0 was a significant rewrite released in the early 2000s (around 2003). Maintenance releases have continued steadily: 4.8 on January 14, 2020, 4.9 on November 6, 2022, and 4.10 on April 21, 2026, which is the latest stable version as of this writing.
BSD sed
The sed shipped with macOS and the BSDs follows the POSIX behavior more conservatively. The most notorious difference is in-place editing: BSD sed -i requires an explicit backup-suffix argument (e.g. sed -i '' 's/a/b/' file), whereas GNU sed -i does not. Portable scripts either avoid -i or account for both dialects.
Current Relevance
Half a century after it was written, sed is everywhere. It is a mandated POSIX utility, preinstalled on essentially every Linux, macOS, and BSD system, and present in minimal environments through BusyBox sed - which is why it is so common inside Docker images and embedded systems.
In day-to-day practice sed remains the default reach-for tool when you need to:
- Do a quick search-and-replace across files or a stream
- Patch configuration during a build, deploy, or container start-up
- Filter, slice, or reformat the output of another command in a pipeline
For heavier data work - fields, arithmetic, associative arrays - practitioners typically graduate to AWK, Perl, or Python. But for the precise, mechanical line transformations that sed was built for, nothing has displaced it.
Why It Matters
sed is a landmark in the history of programming tools. It demonstrated that a small, focused, non-interactive program built around regular expressions could replace whole categories of bespoke utilities. Its substitution syntax (s/old/new/), inherited from ed, propagated through AWK, Perl, and into the regular-expression conventions used by countless languages and editors today.
More than a utility, sed helped define what a Unix “filter” is: a program that reads a stream, transforms it according to a concise script, and writes the result onward. That model of small composable text tools remains one of the most enduring and imitated ideas in software.
Running sed Today
sed is available out of the box on any Unix-like system:
| |
Learning Resources
Books
- sed & awk by Dale Dougherty and Arnold Robbins (O’Reilly) - the classic reference
- Definitive Guide to sed by Daniel Goldman
Online
- GNU sed Manual - https://www.gnu.org/software/sed/manual/
- The sed FAQ - a long-standing community reference for sed dialects and idioms
- grymoire sed tutorial - https://www.grymoire.com/Unix/Sed.html
sed proves that a tool need not be large to be lasting. A compact stream editor conceived at Bell Labs in the early 1970s, it still runs - unchanged in spirit - in the pipelines, build scripts, and containers that power modern computing.
Timeline
Notable Uses & Legacy
Shell Scripting & Automation
sed is a default tool in virtually every Unix-like system, used in shell scripts for find-and-replace, line filtering, and text munging without opening an editor.
Build & Configuration Pipelines
Makefiles, installation scripts, and config-management tools use sed to template files, patch settings, and rewrite paths during builds and deployments.
Log & Data Processing
System administrators chain sed with grep, awk, and sort to clean, reformat, and extract fields from log files and structured text streams.
Source Code Refactoring
Developers use sed for batch transformations across many files - renaming symbols, updating license headers, and applying mechanical edits via pipelines or find -exec.
CI/CD & Containers
Continuous integration scripts and Dockerfiles rely on sed to inject version numbers, edit environment files, and adjust configuration at image build time.
Language Influence
Influenced By
Influenced
Running Today
Run examples using the official Docker image:
docker pull alpine:latestExample usage:
docker run --rm -v $(pwd):/app -w /app alpine:latest sed 's/foo/bar/' input.txt