Est. 1974 Intermediate

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

Paradigm Domain-specific, Stream-oriented, Declarative
Typing Untyped (text streams)
First Appeared 1974
Latest Version GNU sed 4.10 (2026)

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, and awk.

How sed Works

sed processes input through a simple but powerful cycle:

  1. Read one line of input into a buffer called the pattern space (the trailing newline is stripped).
  2. Apply each command in the script, in order, to the pattern space - subject to optional addresses that restrict which lines a command affects.
  3. Unless auto-print is suppressed (-n), print the pattern space.
  4. 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:

1
2
3
4
5
3d              # delete line 3
2,5d            # delete lines 2 through 5
/error/d        # delete every line containing "error"
/start/,/end/p  # print the range from a "start" line to an "end" line
$d              # delete the last line

Key Features

Substitution

The s command is sed’s workhorse and by far its most common use:

1
2
3
4
s/old/new/        # replace first "old" on each line with "new"
s/old/new/g       # replace all occurrences (global)
s/old/new/gi      # global, case-insensitive
s/\(\w*\) \(\w*\)/\2 \1/   # swap two words using capture groups

The & metacharacter refers to the whole match, and \1, \2, … refer to captured groups:

1
s/[0-9]\+/[&]/g   # wrap every number in brackets

Core Commands

CommandAction
s/re/repl/Substitute text matching a regular expression
pPrint the pattern space
dDelete the pattern space and start the next cycle
a, i, cAppend, insert, or change lines of text
y/abc/xyz/Transliterate characters (like tr)
n, NRead the next line into (or append it to) the pattern space
h, H, g, G, xMove data between pattern and hold spaces
b, t, :labelBranch, conditional branch, and labels for loops
qQuit

Common One-Liners

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Replace text in a stream
echo "hello world" | sed 's/world/sed/'

# Print only lines 10-20 of a file
sed -n '10,20p' file.txt

# Delete blank lines
sed '/^$/d' file.txt

# Number every line
sed = file.txt | sed 'N;s/\n/\t/'

# In-place edit (GNU sed)
sed -i 's/foo/bar/g' config.txt

# Print only matching lines (like grep)
sed -n '/pattern/p' file.txt

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:

  • -i for in-place editing of files
  • -E / -r for extended regular expressions
  • -z for 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:

1
2
3
4
5
6
7
8
# Linux (GNU sed)
sed 's/foo/bar/g' file.txt

# macOS / BSD (note the '' for -i)
sed -i '' 's/foo/bar/g' file.txt

# Docker (BusyBox sed in Alpine)
docker run --rm -v $(pwd):/app -w /app alpine:latest sed 's/foo/bar/' input.txt

Learning Resources

Books

  • sed & awk by Dale Dougherty and Arnold Robbins (O’Reilly) - the classic reference
  • Definitive Guide to sed by Daniel Goldman

Online

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

1973-1974
sed developed by Lee E. McMahon at AT&T Bell Labs as a generalized, non-interactive stream editor based on the ed line editor
1979
sed distributed publicly as part of Version 7 Unix, spreading it across the early Unix world
1989
GNU sed development begins as the Free Software Foundation reimplements the tool for GNU/Linux systems (approximate)
1992
sed's behavior standardized by POSIX (IEEE Std 1003.2-1992), later carried into the Single UNIX Specification
2003
GNU sed 4.0 released, a significant rewrite consolidating GNU extensions such as in-place editing (-i) (approximate)
2020
GNU sed 4.8 released on January 14, 2020
2022
GNU sed 4.9 released on November 6, 2022
2026
GNU sed 4.10 released on April 21, 2026, the latest stable version as of mid-2026

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

ed qed grep

Influenced

Running Today

Run examples using the official Docker image:

docker pull alpine:latest

Example usage:

docker run --rm -v $(pwd):/app -w /app alpine:latest sed 's/foo/bar/' input.txt
Last updated: