Misplaced Pages

Sed

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

This is an old revision of this page, as edited by CatherineMunro (talk | contribs) at 23:32, 25 March 2003 (disamb pipe). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Revision as of 23:32, 25 March 2003 by CatherineMunro (talk | contribs) (disamb pipe)(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Sed (which stands for Stream EDitor) is a program used to modify text files. It reads input files line by line, transforms these lines according to rules specified in a certain simple language, and outputs them. The rules often involve regular expressions. While originally a UNIX utility (written by Lee E. McMahon of Bell Labs), it is now available for virtually any operating system which supports a command line.

The following example shows a typical usage of sed:

sed -e 's/OldExpression/NewExpression/g' inputFileName > outputFileName

The s stands for substitute, the g stands for global. That means in the whole line. After the first slash there is the expression to search for and after the second slash there is the expression to substitute instead. The first expression can be a regular expression.

Under Unix this can be combined with a pipe

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

That is, generate the data, but make the small change of replacing x by 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 > outputFileName

Besides substitution other forms of simple processing are possible. For example the following script deletes empty lines or lines which only contain spaces:

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

This example used some of the following metacharacters:

  • ^ Matches the beginning of the line
  • $ Matches the end of the line
  • . Matches any single character
  • * Will match zero or more occurrences of the previous character
  • Matches all the characters inside the

Sed is one of the very early unix commands that permitted command line processing of data files. Cousin to the later AWK, sed allowed powerful and interesting data processing to be done by shell scripts. Sed was probably the earliest Unix tool that really encouraged regular expressions to be used ubiquitously.

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.

External links:


In Egyptian mythology, Sed was a god of redemption.

Alternative: Sedim

Sed Add topic