sed (short for stream editor) is a utility that transforms text via a script written in a relatively simple and compact programming language. It was developed from 1973 to 1974 by Lee E. McMahon of Bell Labs,

and is available today for most operating systems. The functionality of sed is based on the scripting features of the interactive editor ed ("editor", 1971) and the earlier qed ("quick editor", 1965–66). 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 text manipulation and stream editing include AWK and Perl. The shell command that runs the utility has the same name: .

History

First appearing in Version 7 Unix, sed is one of the early Unix utilities built for command line processing of data files. It evolved as the natural successor to the popular grep command. The original motivation was an analogue of grep (g/re/p) for substitution, hence "g/re/s". 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.

Processing

sed reads text, line by line, from an input stream (such as a file) into an internal buffer called the pattern space. As specified via a script, sed applies commands (called actions in sed documentation) to the pattern space. Unless directed otherwise, sed then outputs the pattern space (the modified line) and begins the cycle again with the next line. Other end-of-script behaviors are available via command-line options and via script commands such as <code>d</code> to delete the pattern space, <code>q</code> to quit, <code>N</code> to add the next line to the pattern space immediately. Thus, a sed script corresponds to the body of a loop that iterates through the lines of a stream, where the loop itself and the loop variable (the current line number) are implicit and maintained by sed.

Because the iteration over input lines, variables (pattern space and hold space), input and output streams, and default actions (copy line to pattern space, print pattern space) are implicit, it is possible to write terse one-liner programs. For example, the script prints the first 10 lines of input, then stops.

Use

Conditional execution

Commands accept an optional address argument in terms as a line number or regular expression. The address determines when the command applies. For example, <code>2d</code> would run the <code>d</code> (delete) command on the second input line, while <code>/^ /d</code> would delete all lines beginning with a space. A separate special buffer, the hold space, may be used by a few commands to hold and accumulate text between cycles. The language provides only two variables (the "hold space" and the "pattern space") and GOTO-like branching functionality; nevertheless, the language is Turing-complete, and esoteric sed scripts exist for games such as sokoban, arkanoid, chess, and tetris.

Matching

sed supports regular expression syntax for matching input text to a pattern. For example, the script uses the d command to filter out lines that only contain spaces, or only contain the end of line character.

Supported regular expression metacharacters include:

; caret (<code>^</code>): Matches the beginning of the line.

; dollar sign (<code>$</code>): Matches the end of the line.

; asterisk (<code>*</code>): Matches zero or more occurrences of the previous character.

; plus (<code>+</code>): Matches one or more occurrence(s) of the previous character.

; question mark (<code>?</code>): Matches zero or one occurrence of the previous character.

; dot (<code>.</code>): Matches exactly one character.

Substitution

The original motivation for sed was substitution.