sed (which stands for Stream EDitor) is a simple but powerful computer program used to apply various pre-specified textual transformations to a sequential stream of text data. It reads input files line by line, edits each line according to rules specified in its simple language (the sed script), and then outputs the line. While originally created as a Unix utility by Lee E. McMahon of Bell Labs from 1973 to 1974, sed is now available for virtually every operating system that supports a command line.
sed's command set is modeled after the ed editor, and most commands work similarly in this inverted paradigm. For example, the command 25d means if this is line 25, then delete (don't output) it, rather than go to line 25 and delete it as it does in ed. The notable exceptions are the copy and move commands, which span a range of lines and thus don't have straightforward equivalents in sed. Instead, sed introduces an extra buffer called the hold space, and additional commands to manipulate it. The ed command to copy line 25 to line 76 (25t76) for example would be coded as two separate commands in sed (25h; 76g), to store the line in the hold space until the point at which it should be retrieved.
The s stands for substitute; the g stands for global, which means that all matching occurrences in the line would be replaced. After the first slash is the regular expression to search for and after the second slash is the expression to replace it with. The substitute command (s///) is by far the most powerful and most commonly used sed command.
Under Unix, sed is often used as a filter in a pipeline: generate_data | sed -e 's/x/y/' That is, generate the data, but make the small change of replacing x with y.
Several substitutions or other commands can be put together in a file called, for example, subst.sed and then be applied using the -f option to read the commands from the file: sed -f subst.sed inputFileName > outputFileName
Besides substitution, other forms of simple processing are possible. For example, the following deletes empty lines or lines that only contain spaces: sed -e '/^ *$/d' inputFileName
This example used some of the following regular expression metacharacters:
^) matches the beginning of the line.
$) matches the end of the line.
.) matches any single character.
*) matches zero or more occurrences of the previous character.
and matches any of the characters inside the brackets.
Complex sed constructs are possible, to the extent that it can be conceived of as a highly specialised, albeit simple, programming language. Flow of control, for example, can be managed by use of a label (a colon followed by a string which is to be the label name) and the branch instruction b; an instruction b followed by a valid label name will move processing to the block following the label; if the label does not exist then the branch will end the script.
sed and AWK are often cited as the progenitors and inspiration for Perl; in particular the s/// syntax from the example above is part of Perl's syntax.
sed's language does not have variables and has only primitive GOTO and branching functionality; nevertheless, the language is Turing-complete.
GNU sed includes several new features such as in-place editing of files (i.e., replace the original file with the result of applying the sed program). In-place editing is often used instead of ed scripts: for example,
sed -i 's/abc/def/' file
can be used instead of
ed file 1,$ s/abc/def/ w q
There is an extended version of sed called Super-sed (ssed) that includes regular expressions compatible with Perl.
Another version of sed is minised, originally reverse-engineered from the 4.1BSD sed by Eric S. Raymond and currently maintained by René Rebe. minised was used by the GNU project until the GNU project wrote a new version of sed based on the new GNU regular expression library. The current minised contains some extensions to BSD sed but is not as feature-rich as GNU sed. Its advantage is that it is very fast and uses little memory. It is used on embedded systems and is the version of sed provided with Minix.
Consider the following text: This is my cat my cat's name is betty This is my dog my dog's name is frank
The sed script below will turn it into: This is my cat my cat's name is betty This is my dog my dog's name is frank
Here's the script: sed 'N;s/\n / /;P;D;'
More complex substitutions are possible using the "Address" command: /pattern1/s/pattern2/replacement/flags
For example, if you have a file (text.txt) containing the following lines:
Hello world. Hello world. I love sed.
And you want to replace "world" with "mom", but only on those lines that contain the word "sed", you can use: sed -e '/^.*sed.*$/s/world/mom/g' text.txt will result in: Hello world. Hello mom. I love sed.
You can negate this behavior with: sed -e '/^.*sed.*$/!s/world/mom/g' text.txt which will result in the opposite: Hello mom. Hello world. I love sed.
Domain-specific programming languages | Text-oriented programming languages | Scripting languages | Unix software | Free compilers and interpreters
Sed | Sed | Sed | Sed (Unix) | Sed | Sed (logiciel) | Stream Editor | Sed (コンピュータ) | Sed | Sed | Sed | Sed