Sed (software)

sed (abbreviation of S tream ED itor, “editor of flow”) is a Computer program simple but powerful, making it possible to apply various preset transformations to a sequential flow of textual data. sed reads data input line by line, modifies each line according to rules specified in a clean language (called “script sed”), then turns over this line. Although originally written for Unix, by Lee E. McMahon in 1973/1974 (Beautiful Labs), sed is now available on practically all the operating systems having an interface in Ligne of order.

Presentation

sed is often described like a text editor not-interactive. It differs from a conventional editor in this that the treatment sequence of two flows of information necessary (data and instructions) is reversed. Instead of one by one taking the format controls to apply them to the entirety of the text (which must then be completely in memory), sed traverses only once the textual file, by applying the whole of the format controls to each line. As only one line at the same time is present in memory, sed can treat files of completely arbitrary size.

Principle of operation

The command set of sed is based on that of the editor ED. Indeed, the majority of the orders function in a similar way, in spite of the inversion of the paradigm. For example, the order 25d means then if it is about line 25, erases to it (i.e., by worms the exit does not return it) , instead of goes to line 25 and erases to it , such as ED carries out it. The notable exceptions are the orders of copy and displacement, which apply to an interval of lines, and which consequently do not have a direct equivalent in sed. In the place, sed introduces a additional Buffer memory called hold space , as well as orders to handle it.

For example, the order ED to copy line 25 with line 76 (25t76) could be coded in two distinct orders in sed (25h; 76g). The first memorizes the line in the hold space , the second recovers it when it is time.

Use

The following example shows a usual use of sed: sed - E “s/Ancien/Nouveau/g” nomFichierEntrée > nomFichierSortie

The order s means substitute (“to substitute”). The flag g means total , which indicates that all the occurrences of each line must be replaced. After the first character / is given a rational expression that sed must find. After the second / is specified the expression to replace what it found. The ordering of substitution (s///) is by far the most powerful order of sed and most frequently used.

A possible application of the subsitution is the correction of a recurring spelling error, or the replacement of all the occurrences of initials. For example: sed - E “s/fichier/fichier/g” sed.wikipedia > sed.wikipedia.correct sed - E “s/WP/Wikipedia/g” sed.wikipedia > sed.wikipedia.correct

It is not obligatory to use “/” like delimitor. Characters “#, -. ; ” (nonexhaustive list) can also be used for to avoid the accumulation of backslash of “protection” making syntax not very readable. The following orders are equivalent: S \ /un \ /chemin \ /vers/ \ /une \ /autre \ /chaine/

s#/un/chemin/vers#/une/autre/chaine#

sed can also be used for numbering the lines of a file, removing the beacons HTML of a text, etc However, on certain platforms, the language Perl is very often used to replace sed.

On Unix, sed is often used like filter in a tube: production_de_données | sed - E “s/x/y/” In this example, data are generated, then they are modified with stolen by replacing the X by there .

Several substitutions, or other orders, can be gathered in a file, called script sed (subst.sed in the following example), then applied to the data: sed - F subst.sed nomFichierEntrée > nomFichierSortie

In addition to substitutions, other types of simple treatments are available. For example, following script withdraws the blank lines or which contain only spaces: sed - E “/^ *$/d' nomFichierEntrée This example uses Méta-character S to form rational Expressions:

Sed also allows the use of the fields to identify certain parts of a character string. A field is defined by a rational expression identified by the beacons \ ( and \) , the field can then be used with \ n where n is the number of the field (the number of fields is limited to 9). Example: to permute two fields separated by an indent:

S \ (. * \) - \ (. * \)/\ 2 \ 1

Another example: to post the NR the last characters of a chain:

S \ (. * \) \ (. \ {NR \} \)/\ 2

Two fields are defined: \ 1 which will contain the first characters and \ 2 which will contain the NR the last. It will be noted that the accodances of the rational expression which make it possible to specify the number of occurrences {NR} must be protected by a backslash.

Programming

The complex structures are possible with sed, insofar as it can be comparable with a Computer programming language very specialized but simple. For example, the control of the wire of execution can be managed using labels ( labels , a : followed name of the label) and instruction branch b. An instruction b followed by a valid name of label will tranférera the wire of execution to the block following the label. If the label does not exist, then the instruction finishes script.

sed is one of the oldest Unix orders making it possible the Ligne of order to treat data files. It naturally evolved/moved like successor of the famous order Grep. The cousin of youngest the Awk, sed allows Shell script to carry out powerful and very useful treatments. sed was probably one of the very first tools of Unix to really encouraging the omnipresent use of the rational expressions. With regard to the speed of treatment, sed is generally faster than Perl, and appreciably faster than Awk.

sed and Awk are often quoted as being the ancestors and the inspirers of Perl. In particular, syntax s/// of the preceding example forms integral part of the syntax of Perl.

The language of sed does not have variable and has only a primitive Goto like instruction of control of flow. However, it is Turing-complete.

Extensions

GNU sed integrates several new functionalities such as the edition “in place” (the original file is replaced by the result of the treatment by sed). The edition in-place is often used to replace a script ED. For example: sed - I “s/abc/def/” file can be used in the place of ED file 1, $ s/abc/def/ W Q

There exists a version improved of sed, called super-sed (Ssed), which integrates rational expressions compatible with Perl.

Example

The example which follows watch how sed, which separately treats usually each line, can remove the jumps of lines of the lines whose line which follows starts with a space.

To illustrate the desired effect, let us 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 order sed will give: This is my cat my cat' S name is betty This is my dog my dog' S name is frank

Here the order: sed “NR; S \ N//g; P; D; ” who can break up (the in the following way; makes it possible to separate the orders): NR; S \ N//g; P; D;   NR reads (also) the following line S \ N//g carries out substitution… /\ N/… of a jump of line followed by a space… //… by a simple space… G… for all the occurrences P turns over the result of the treatment D erases what remains in order not to post the following line twice

See too

Internal bonds

  • rational Expressions
  • ED
  • Awk
  • Perl

External bonds

  • Official site
  • FAQ of sed
  • Page of handbook GNU of sed
  • brief Documentation
  • Principal site of resources sed
  • sed, in particular for Windows/DOS

Random links:Paramecium | Vault Sixtine de Dürrenmatt | Pertharite (Crow) | XDR DRAM | Rue Dauphine | Paranoid Park | Temps_amok