sed Command

Purpose

A stream editor.

Syntax

sed [ -n ] [ -u ] Script  [ File ... ]

sed [ -n ] [ -u ] [ -e Script ] ... [ -f ScriptFile ] ...  [ File ... ]

Description

The sed command modifies lines from the specified File parameter according to an edit script and writes them to standard output. The sed command includes many features for selecting lines to be modified and making changes only to the selected lines.

The sed command uses two workspaces for holding the line being modified: the pattern space, where the selected line is held; and the hold space, where a line can be stored temporarily.

An edit script consists of individual subcommands, each one on a separate line. The general form of sed subcommands is the following:

[address-range] function[modifiers]

The sed command processes each input File parameter by reading an input line into a pattern space, applying all sed subcommands in sequence whose addresses select that line, and writing the pattern space to standard output. It then clears the pattern space and repeats this process for each line specified in the input File parameter. Some of the sed subcommands use a hold space to save all or part of the pattern space for subsequent retrieval.

When a command includes an address (either a line number or a search pattern), only the addressed line or lines are affected by the command. Otherwise, the command is applied to all lines.

An address is either a decimal line number, a $ (dollar sign), which addresses the last line of input, or a context address. A context address is a regular expression similar to those used in the ed command except for the following differences:

  • You can select the character delimiter for patterns. The general form of the expression is:
    \?pattern?
    where ? (question mark) is a selectable character delimiter. You can select any character from the current locale except for the space or new-line character. The \ (backslash) character is required only for the first occurrence of the ? (question mark).

    The default form for the pattern is the following:

    /pattern/
    A \ (backslash) character is not necessary.
  • The \n sequence matches a new-line character in the pattern space, except the terminating new-line character.
  • A . (period) matches any character except a terminating new-line character. That is, unlike the ed command, which cannot match a new-line character in the middle of a line, the sed command can match a new-line character in the pattern space.

Certain commands called addressed commands allow you to specify one line or a range of lines to which the command should be applied. The following rules apply to addressed commands:

  • A command line without an address selects every line.
  • A command line with one address, expressed in context form, selects each line that matches the address.
  • A command line with two addresses separated by commas selects the entire range from the first line that matches the first address through the next line that matches the second. (If the second address is a number less than or equal to the line number first selected, only one line is selected.) Thereafter, the process is repeated, looking again for the first address.

Flags

Item Description
-e Script Uses the Script variable as the editing script. If you are using just one -e flag and no -f flag, the -e flag can be omitted.
-f ScriptFile Uses the ScriptFile variable as the source of the edit script. The ScriptFile variable is a prepared set of editing commands applied to the File parameter.
-n Suppresses all information normally written to standard output.
-u Displays the output in an unbuffered mode. When this flag is set, the sed command displays the output instantaneously instead of buffering the output. The default is buffered mode.
Note: You can specify multiple -e and -f flags. All subcommands are added to the script in the order specified, regardless of their origin.

sed Subcommands

The sed command contains the following sed script subcommands. The number in parentheses preceding a subcommand indicates the maximum number of permissible addresses for the subcommand.
Note:
  1. The Text variable accompanying the a\, c\, and i\ subcommands can continue onto more than one line, provided all lines but the last end with a \ (backslash) to quote the new-line character. Backslashes in text are treated like backslashes in the replacement string of an s command and can be used to protect initial blanks and tabs against the stripping that is done on every script line. The RFile and WFile variables must end the command line and must be preceded by exactly one blank. Each WFile variable is created before processing begins.
  2. The sed command can process up to 999 subcommands in a pattern file.
Item Description
(1) a\Text Places the Text variable in output before reading the next input line.
(2)b[label] Branches to the : command bearing the label variable. If the label variable is empty, it branches to the end of the script.
(2)c\Text Deletes the pattern space. With 0 or 1 address or at the end of a 2-address range, places the Text variable in output and then starts the next cycle.
(2)d Deletes the pattern space and then starts the next cycle.
(2)D Deletes the initial segment of the pattern space through the first new-line character and then starts the next cycle.
(2)g Replaces the contents of the pattern space with the contents of the hold space.
(2)G Appends the contents of the hold space to the pattern space.
(2)h Replaces the contents of the hold space with the contents of the pattern space.
(2)H Appends the contents of the pattern space to the hold space.
(1)i\Text Writes the Text variable to standard output before reading the next line into the pattern space.
(2)l Writes the pattern space to standard output showing nondisplayable characters as 4-digit hexadecimal values. Long lines are folded.
(2)l Writes the pattern space to standard output in a visually unambiguous form. The characters \\\, \\a, \\b, \\f, \\r, \\t, and \\v are written as the corresponding escape sequence. Non-printable characters are written as 1 three-digit octal number (with a preceding backslash character) for each byte in the character (most significant byte first). This format is also used for multibyte characters. This subcommand folds long lines. A backslash followed by a new-line character indicates the point of folding. Folding occurs at the 72nd column position. A $ (dollar sign) marks the end of each line.
(2)n Writes the pattern space to standard output if the default output is not suppressed. It replaces the pattern space with the next line of input.
(2)N Appends the next line of input to the pattern space with an embedded new-line character (the current line number changes). You can use this to search for patterns that are split onto two lines.
(2)p Writes the pattern space to standard output.
(2)P Writes the initial segment of the pattern space through the first new-line character to standard output.
(1)q Branches to the end of the script. It does not start a new cycle.
(2)r RFile Reads the contents of the RFile variable. It places contents in output before reading the next input line.
(2)s/pattern/replacement/flags Substitutes the replacement string for the first occurrence of the pattern parameter in the pattern space. Any character that is displayed after the s subcommand can substitute for the / (slash) separator except for the space or new-line character.

See the Pattern Matching section of the ed command.

The value of the flags variable must be zero or more of:

g
Substitutes all non-overlapping instances of the pattern parameter rather than just the first one.
n
Substitutes for the n-th occurrence only of the pattern parameter.
p
Writes the pattern space to standard output if a replacement was made.
w WFile
Writes the pattern space to the WFile variable if a replacement was made. Appends the pattern space to the WFile variable. If the WFile variable was not already created by a previous write by this sed script, the sed command creates it.
(2)tlabel Branches to the :label variable in the script file if any substitutions were made since the most recent reading of an input line execution of a t subcommand. If you do not specify the label variable, control transfers to the end of the script.
(2)wWFile Appends the pattern space to the WFile variable.
(2)x Exchanges the contents of the pattern space and the hold space.
(2)y/pattern1/pattern2/ Replaces all occurrences of characters in the pattern1 variable with the corresponding pattern2 characters. The number of characters in the pattern1 and pattern2 variables must be equal. The new-line character is represented by \n.
(2)!sed-cmd Applies the specified sed subcommand only to lines not selected by the address or addresses.
(0):label Marks a branch point to be referenced by the b and t subcommands. This label can be any sequence of eight or fewer bytes.
(1)= Writes the current line number to standard output as a line.
(2){subcmd } Groups subcommands enclosed in {} (braces).
(0) Ignores an empty command.
(0)# The "#" and the remainder of the line are ignored (treated as a comment), with one exception. For the first line of a script file, if the character after the # is an n, the default output is suppressed. The rest of the line after the #n is ignored.

Exit Status

This command returns the following exit values:

Item Description
0 Successful completion.
>0 An error occurred.

Examples

  1. To perform a global change, enter:
    sed  "s/happy/enchanted/g" chap1  >chap1.new
    This command sequence replaces each occurrence of the word happy found in the file chap1 with the word enchanted. It puts the edited version in a separate file named chap1.new. The g character at the end of the s subcommand tells the sed command to make as many substitutions as possible on each line. Without the g character, the sed command replaces only the first occurrence of the word happy on a line.

    The sed command operates as a filter. It reads text from standard input or from the files named on the command line (chap1 in this example), modifies this text, and writes it to standard output. Unlike most editors, it does not replace the original file. This makes the sed command a powerful command when used in pipelines.

  2. To use the sed command as a filter in a pipeline, enter:
    pr  chap2 | sed "s/Page *[0-9]*$/(&)/" | enq
    This command sequence encloses the page numbers in parentheses before printing the file chap2. The pr command puts a heading and page number at the top of each page, then the sed command puts the page numbers in parentheses, and the enq command prints the edited listing.

    The sed command pattern /Page *[0-9]*$/ matches page numbers that appear at the end of a line. The s subcommand changes this to (&), where the & stands for the page number that was matched.

  3. To display selected lines of a file, enter:
    sed  -n  "/food/p" chap3
    The sed -n displays each line in the file chap3 that contains the word food. Normally, the sed command copies every line to standard output after it is edited. The -n flag stops the sed command from doing this. You then use subcommands like p to write specific parts of the text. Without the -n flag, this example displays all the lines in the file chap3, and it shows each line containing food twice.
  4. To perform complex editing, enter:
    sed  -f  script.sed  chap4  >chap4.new
    This command sequence creates a sed script file when you want to do anything complex. You can then test and modify your script before using it. You can also reuse your script to edit other files. Create the script file with an interactive text editor.
  5. A sample sed script file:
    :join
    /\\$/{N
    s/\\\n//
    b join
    }
    This sed script joins each line that ends with a \ (backslash) to the line that follows it. First, the pattern /\\$/ selects a line that ends with a \ for the group of commands enclosed in {} (braces). The N subcommand then appends the next line, embedding a new-line character. The s/\\\n// deletes the \ and embedded new-line character. Finally, b join branches back to the label :join to check for a at the end of the newly joined line. Without the branch, the sed command writes the joined line and reads the next one before checking for a second \.
    Note: The N subcommand causes the sed command to stop immediately if there are no more lines of input (that is, if the N subcommand reads an end-of-file character). It does not copy the pattern space to standard output before stopping. This means that if the last line of the input ends with a \, it is not copied to the output.
  6. To copy an existing file (oldfile) to a new file (newfile) and replace all occurrences of the testpattern text string with the contents of the $REPL shell variable, enter:
    cat oldfile | sed -e "s/testpattern/$REPL/g" > newfile
  7. To replace all occurrences of A with a, B with b, C with c, and all occurrences of newlines with character Z in the input file, enter:
    $ sed -f command.file input.file
    where command.file is the script file and input.file is the input file.
       $cat command.file
       y/ABC\n/abcZ/
    Alternatively, the following command can also be executed for the same function:
    sed "y/ABC\n/abcZ/" input.file