Sed is a non-interactive
[1]
line editor. It receives text input, whether from
Sed determines which lines of its input that it will
operate on from the address range passed
to it.
[2]
Specify this address range either by line number or by a
pattern to match. For example,
Of all the operations in the sed toolkit, we will focus
primarily on the three most commonly used
ones. These are printing (to
Table C-1. Basic sed operators
| Operator | Name | Effect |
|---|---|---|
| Print [specified address range] | ||
| delete | Delete [specified address range] | |
| substitute | Substitute pattern2 for first instance of pattern1 in a line | |
| substitute | Substitute pattern2 for first instance of pattern1 in a
line, over | |
| transform | replace any character in pattern1 with the
corresponding character in pattern2, over
| |
| global | Operate on every pattern match within each matched line of input |
![]() | Unless the |
From the command line and in a shell script, a sed operation may require quoting and certain options.
sed -e '/^$/d' $filename
# The -e option causes the next string to be interpreted as an editing instruction.
# (If passing only a single instruction to sed, the "-e" is optional.)
# The "strong" quotes ('') protect the RE characters in the instruction
#+ from reinterpretation as special characters by the body of the script.
# (This reserves RE expansion of the instruction for sed.)
#
# Operates on the text contained in file $filename. |
In certain cases, a sed editing command will not work with single quotes.
filename=file1.txt
pattern=BEGIN
sed "/^$pattern/d" "$filename" # Works as specified.
# sed '/^$pattern/d' "$filename" has unexpected results.
# In this instance, with strong quoting (' ... '),
#+ "$pattern" will not expand to "BEGIN". |
![]() | Sed uses the |
sed -n '/xzy/p' $filename # The -n option tells sed to print only those lines matching the pattern. # Otherwise all input lines would print. # The -e option not necessary here since there is only a single editing instruction. |
Table C-2. Examples of sed operators
| Notation | Effect |
|---|---|
| Delete 8th line of input. | |
| Delete all blank lines. | |
| Delete from beginning of input up to, and including first blank line. | |
| Print only lines containing "Jones" (with -n option). | |
| Substitute "Linux" for first instance of "Windows" found in each input line. | |
| Substitute "stability" for every instance of "BSOD" found in each input line. | |
| Delete all spaces at the end of every line. | |
| Compress all consecutive sequences of zeroes into a single zero. | |
| Delete all lines containing "GUI". | |
| Delete all instances of "GUI", leaving the remainder of each line intact. |
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
|
|
A backslash forces the sed replacement command to continue on to the next line. This has the effect of using the newline at the end of the first line as the replacement string.
s/^ */\ /g |
An address range followed by one or more operations may require open and closed curly brackets, with appropriate newlines.
/[0-9A-Za-z]/,/^$/{
/^$/d
} |
![]() | The usual delimiter that sed uses is /. However, sed allows other delimiters, such as %. This is useful when / is part of a replacement string, as in a file pathname. See Example 10-9 and Example 15-32. |
![]() | A quick way to double-space a text file is |
For illustrative examples of sed within shell scripts, see:
For a more extensive treatment of sed, check the appropriate references in the Bibliography.
| [1] | Sed executes without user intervention. |
| [2] | If no address range is specified, the default is all lines. |