Sed is often thought of as a non-interactive text editor. It differs from conventional text editors in that the processing of the two inputs is inverted. Instead of iterating once through a list of edit commands applying each one to the whole text file in memory, sed iterates once through the text file applying the whole list of edit commands to each line. Because only one line at a time is in memory, sed can process arbitrarily-large text files.
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 exception is the copy and move commands, which span a range of lines and thus don't have straight-forward 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 following example shows a typical usage of sed:
sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileNameThe s stands for substitute; the g stands for global. That means in the whole line. 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 like
sed -f subst.sed inputFileName > outputFileNameBesides substitution, other forms of simple processing are possible. For example the following script deletes empty lines or lines that only contain spaces:
sed -e '/^ *$/d' inputFileNameThis example used some of the following metacharacters:
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 only primitive GOTO and branching functionality; nevertheless, the language is Turing-complete.
There is an extended version of sed called Super-sed that includes several new features such as in-place editing of files.
In Egyptian mythology, Sed was a god of redemption.
Alternative: SedimExternal links: