(M)  s i s t e m a   o p e r a c i o n a l   m a g n u x   l i n u x ~/ · documentação · suporte · sobre

 

B.1. Sed

Sed is a non-interactive line editor. It receives text input, whether from stdin or from a file, performs certain operations on specified lines of the input, then outputs the result to stdout or to a file. Within a shell script, sed is usually one of several tool components in a pipe.

Sed determines which lines of its input that it will operate on from the address range passed to it. Specify this address range either by line number or by a pattern to match. For example, 3d signals sed to delete line 3 of the input, and /windows/d tells sed that you want every line of the input containing a match to "windows" deleted.

Of all the operations in the sed toolkit, we will focus primarily on the three most commonly used ones. These are printing (to stdout), deletion, and substitution.

Table B-1. sed operators

OperatorNameEffect
/address-range/pprintPrint (specified address range)
/address-range/ddeleteDelete (specified address range)
s/pattern1/pattern2/substituteSubstitute pattern2 for pattern1
/address-range/y/pattern1/pattern2/transformreplace pattern1 with pattern2 (works just like tr)
gglobalOperate on every pattern match within each matched line of input

Note: Unless the g (global) operator is appended to a substitute command, the substitution operates only on the first instance of a pattern match within each line.

From the command line and in a shell script, a sed operation may require quoting and certain options.

sed -e '/^$/d'
# The -e option causes the next string to be interpreted as an instruction.       
# The "strong" quotes ('') protect the special characters in the instruction
# from reinterpretation as regular expressions by the body of the script.
# (This reserves RE expansion of the instruction for sed.)

Note: Both sed and awk use the -e option to specify that the following string is an instruction or set of instructions. If there is only a single instruction contained within the string, then the option may be omitted.

sed -n '/xzy/p'
# The -n option tells sed to print only those lines matching the pattern.
# Otherwise all input lines would print.

Table B-2. examples

NotationEffect
8dDelete 8th line of input.
/^$/dDelete all blank lines.
1,/^$/dDelete from beginning of input up to, and including first blank line.
/Jones/pPrint only lines containing "Jones" (with -n option).
s/Windows/Linux/Substitute "Linux" for first instance of"Windows" found in each input line.
s/BSOD/stability/gSubstitute "stability" for every instance of"BSOD" found in each input line.
/GUI/dDelete all lines containing "GUI".
s/GUI//gDelete all instances of "GUI", leaving the remainder of each line intact.

Note: Substituting a zero-length string for another is equivalent to deleting that string within a line of input. This leaves the remainder of the line intact. Applying s/GUI// to the line The most important parts of any application are its GUI and sound effects results in

The most important parts of any application are its  and sound effects
.

For illustrative examples of sed within shell scripts, see:

  1. Example 2-3

  2. Example 2-4

  3. Example 3-47

  4. Example A-2

  5. Example 3-55

  6. Example 3-60

  7. Example A-4

  8. Example A-8

  9. Example 3-65

For a more extensive treatment of sed, check the appropriate references in the Bibliography.