Sed

related topics
{math, number, function}
{system, computer, user}
{woman, child, man}
{language, word, form}
{game, team, player}
{style, bgcolor, rowspan}

sed (stream editor) is a Unix utility that parses text files and implements a programming language which can apply textual transformations to such files. It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line. It was developed from 1973 to 1974 as a Unix utility by Lee E. McMahon of Bell Labs,[1] and is available today for most operating systems.[2]

Contents

History

sed is one of the very early Unix commands built for command line processing of data files. It evolved as the natural successor to the popular grep command.[3] Cousin to the later AWK, sed allowed powerful and interesting data processing to be done by shell scripts.

sed and AWK are often cited as the progenitors and inspiration for Perl. The s / / / syntax shown below is part of Perl's syntax and originated with ed, the precursor to sed.

sed's language does not have variables and has only primitive GOTO and branching functionality; nevertheless, the language is Turing-complete.[3] [4]

GNU sed added several new features. The best-known is in-place editing of files (i.e., replace the original file with the result of applying the sed program), which was later included in BSD sed too. This feature is nowadays often used instead of ed scripts: for example,

sed -i 's/abc/def/' file

can often be used instead of

ed file
1,$ s/abc/def/
w
q

Note: "sed -i" overwrites the original file with a new one, breaking any links the original may have had, while the above example using "ed" changes only the original file's contents, preserving file links.

Super-sed is an extended version of sed that includes regular expressions compatible with Perl.

Another variant 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.[citation needed] It is used on embedded systems and is the version of sed provided with Minix.[citation needed]

[edit] Usage

The following example shows a typical use of sed, where the -e option indicates that the sed expression follows:

sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileName

In many versions, the -e is not required to precede the expression. The s stands for substitute. The g stands for global, which means that all matching occurrences in the line would be replaced. The regular expression (i.e. pattern) to be searched is placed after the first delimiting symbol (slash here) and the replacement follows the second symbol. Slash is the conventional symbol. Any other could be used to make syntax more readable if it does not occur in the pattern or replacement (see below), which is useful to avoid leaning toothpick syndrome.

Under Unix, sed is often used as a filter in a pipeline:

generate_data | sed -e 's/x/y/g'

That is, generate the data, and then 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 uses the d command to delete lines that are either blank or only contain spaces:

sed -e '/^ *$/d' inputFileName 

This example used some of the following regular expression metacharacters:

Full article ▸

related documents
Jess programming language
Blum Blum Shub
Normal morphism
Sophie Germain prime
Axiom of union
Baire category theorem
Double precision
Contraction mapping
Multiple inheritance
LALR parser
Complete measure
Mutual recursion
Nearest neighbour algorithm
Characteristic subgroup
Best-first search
Lyapunov fractal
Tree structure
Hamiltonian path problem
Up to
Complete category
Identifier
Category of sets
Brun's constant
Bookmarklet
Group homomorphism
Elias delta coding
Composite number
AVL tree
Partition of unity
Linear congruence theorem