sed ("stream editor") is a Unix utility that parses and transforms text, using a simple, compact programming language.
It was one of the earliest tools to support regular expressions, and remains in use for text processing, most notably with the substitution command.
Popular alternative tools for plaintext string manipulation and "stream editing" include AWK and Perl.
[3] Foreseeing that further special-purpose programs for each command would also arise, such as g/re/d, McMahon wrote a general-purpose line-oriented stream editor, which became sed.
Commands in the sed script may take an optional address, in terms of line numbers or regular expressions.
sed's command language has only two variables (the "hold space" and the "pattern space") and GOTO-like branching functionality; nevertheless, the language is Turing-complete,[6][7] and esoteric sed scripts exist for games such as sokoban, arkanoid,[8] chess,[9] and tetris.
Lines of a sed script are each a pattern-action pair, indicating what pattern to match and which action to perform, which can be recast as a conditional statement.
Slash (/) is the conventional symbol, originating in the character for "search" in ed, but any other could be used to make syntax more readable if it does not occur in the pattern or replacement; this is useful to avoid "leaning toothpick syndrome".
The substitution command, which originates in search-and-replace in ed, implements simple parsing and templating.
The regexp provides both pattern matching and saving text via sub-expressions, while the replacement can be either literal text, or a format string containing the characters & for "entire match" or the special escape sequences \1 through \9 for the nth saved sub-expression.
For example, the following uses the d command to filter out lines that only contain spaces, or only contain the end of line character: This example uses some of the following regular expression metacharacters (sed supports the full range of regular expressions): Complex sed constructs are possible, allowing it to serve as a simple, but highly specialized, programming language.
Additionally, the { instruction starts a subsequence of commands (up to the matching }); in most cases, it will be conditioned by an address pattern.
For the specific tasks they are designed to carry out, such specialized utilities are usually simpler, clearer, and faster than a more general solution such as sed.
An analog to ed/sed is sam/ssam, where sam is the Plan 9 editor, and ssam is a stream interface to it, yielding functionality similar to sed.