sed

Stream editor (POSIX)

Syntax:

sed [options]... {script-only-if-no-other-script} [input-file]...

Runs on:

QNX Neutrino, Microsoft Windows

Options:

-e script or --expression=script
Use the command-line script for editing input-files. If you specify multiple -e options, the scripts are applied in the order specified to each line of the input files. If a -f option is specified in addition to -e, lines are acted upon by scripts first.
-f script_file or --file=script-file
Use the file script_file as the script of editing instructions. If multiple -f options are specified, the scripts are applied in the order specified to each line of the input files. If a -e option is specified in addition to -f, lines are acted upon by scripts first.
--help
Display some help, and then exit.
-i[suffix] or --in-place[=suffix]
Edit files in place (making a backup if you specify the suffix).
-l N or --line-length=N
("el") Specify the desired line-wrap length for the l command.
-n or --quiet or --silent
Suppress the default output, which passes each line to standard output after it's examined for editing. Only lines explicitly selected for output are written.
--posix
Disable all GNU extensions.
-r or --regexp-extended
Use extended regular expressions in the script.
-s or --separate
Consider files as separate rather than as a single continuous long stream.
-u or --unbuffered
Load minimal amounts of data from the input files and flush the output buffers more often.
--version
Display the version number, and then exit.

If you don't specify any -e, --expression, -f, or --file options, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if you don't specify any input files, then the standard input is read.

Description:

The sed utility is a stream editor that reads one or more text files, makes editing changes according to editing commands that you specify, and writes the results to standard output.

Note: This utility is subject to the GNU Public License (GPL). We've included it for use on development systems.

The sed commands are usually stored in a program file (script_file), although you may give simple sed commands from the command line. By default, sed copies files from the file list to its standard output, editing the lines in the process. Conceptually, there is one input file, which is the concatenation of all input files specified.

Lines are selected for editing based on their position within the input file, or by pattern matching. If no files are listed, input is taken from standard input (this is the only time standard input is used). The sed utility initially reads all the editing commands from all specified sources and places them in an internal table in the order specified. The utility then processes the (concatenated) input file as follows:

  1. Read one line from the input file and copy to the pattern space (a work area).
  2. For each command that selects the line, act on the pattern space as the edit command specifies, cyclically placing the result into the pattern space.
  3. After all commands have been checked against the pattern space, write the pattern space to the standard output, provided -n was not specified. The pattern space may contain zero, one, or several lines at this time.
  4. Delete the contents of the pattern space.
  5. Repeat from step 1 until all of the (concatenated) input file has been read and processed.

Scripts

A script consists of editing commands, one per line, of the following form:

[address[,address]]function[arguments]

You can specify the script of editing commands in the command line, or it can be contained in the file script_file.

In default operation, sed cyclically copies a line of input into a pattern space (unless there is something left after a D command), applies in sequence all commands whose addresses select that pattern space, and at the end of the script copies the pattern space to the standard output (except under -n) and deletes the pattern space.

Some of the commands use a hold space to save all or part of the pattern space for subsequent retrieval. The pattern and hold spaces are each limited to 20 KB.

Addresses

An address is one of the following:

A command line with no address selects every pattern space. A command line with one address selects each pattern space that matches the address.

A command line with two addresses selects the inclusive range from the first pattern space that matches the first address through the next pattern space that matches the second address. (If the line number of the second address is less than or equal to the line number first selected, then only the first line is selected.) Starting at the first line following the selected range, sed looks again for the first address. Thereafter the process is repeated.

You can use the negation character (!) to apply editing commands to non-selected pattern spaces. For more information, see "Editing commands," below.

Regular expressions

The sed utility uses basic regular expressions (REs), with the following additions:

Editing commands

In the following list of functions, the maximum number of permissible addresses for each function is indicated by one of [0addr], [1addr] or [2addr] representing a maximum of zero addresses, one address or two addresses respectively. The argument text consists of one or more lines. Each embedded newline in the text must be preceded by a backslash. Other backslashes in text are treated like the backslashes in the replacement string of an s editing command; you can use them to protect initial blanks against the stripping that's done on every script line.

The r and w editing commands take an optional rfile (or wfile) parameter, separated from the command letter by zero or more blanks.

The arguments rfile or wfile terminate the command line. Each wfile is created before processing begins. There can be at most ten distinct wfile arguments in the script.

The b, r, s,t, w, y, !, and : editing commands take additional arguments. The following synopses indicate which arguments are separated from the commands by blanks:

[2addr] { command_list or }
Execute command_list only when the pattern space is selected. (Note that the trailing } must be the first non-blank character in the line.)
[1addr]a\ or text
Write text to the standard output after the pattern space is written.
[2addr]b label
Branch to the : (colon) command bearing the label. If label is empty, branch to the end of the script.
[2addr]c\ or text
Delete the pattern space. With 0 or 1 address or at the end of a 2-address range, place text on the output.
[2addr]d
Delete the pattern space and start the next cycle.
[2addr]D
Delete the initial segment of the pattern space through the first newline and start the next cycle.
[2addr]g
Replace the contents of the pattern space by the contents of the hold space.
[2addr]G
Append the contents of the hold space to the pattern space.
[2addr]h
Replace the contents of the hold space by the contents of the pattern space.
[2addr]H
Append the contents of the pattern space to the hold space.
[1addr]i\ or text
Write text to the standard output before the pattern space is written.
[2addr]l
List the pattern space on the standard output in an unambiguous form. Nonprinting characters are listed as hexadecimal digit pairs, with a preceding backslash, with the following exceptions:
Character Listed as
alert \a
backslash \\
backspace \b
carriage return \r
form-feed \f
newline \n
tab \t
vertical tab \v
Long lines are folded; the length at which folding occurs is unspecified, but should be appropriate for the output device.
[2addr]n
Copy the pattern space to the standard output and replace the pattern space with the next line of input.
[2addr]N
Append the next line of input to the pattern space, using an embedded newline to separate the appended material from the original material. Note that the current line number changes.
[2addr]p
Copy (print) the pattern space to the standard output.
[2addr]P
Copy (print) the pattern space, up to the first newline, to the standard output.
[1addr]q
Branch to the end of the script and quit without starting a new cycle.
[1addr]r rfile
Read the contents of the rfile file. The contents are placed on the output before reading the next input line.
[2addr]s/regular expression/replacement string/flags
Substitute the replacement string for instances of regular expression in the pattern space. You can use any character instead of /. The value of flags is zero or more of:
n
n=1 to 512. Substitute for the nth occurrence only of the regular expression found within the pattern space.
g
Globally substitute for all non-overlapping instances of the regular expression rather than just the first one. If both g and n are specified, g takes precedence.
p
Print the pattern space if a replacement was made.
w wfile
Append (write) the pattern space to wfile if a replacement was made.
[2addr]t label
Test; branch to the : (colon) command bearing the label if any substitutions have been made since the most recent reading of an input line or execution of a t. If label is empty, branch to the end of the script.
[2addr]w wfile
Append (write) the pattern space to wfile.
[2addr]x
Exchange the contents of the pattern and hold spaces.
[2addr]y/string1/string2/
Replace all occurrences of collating elements in string1 with the corresponding collating element in string2. The lengths of string1 and string2 should be equal.
[2addr]! function
Apply the function (or command list, if function is {) only to the lines that aren't selected by the addresses.
[0addr]:label
This command does nothing; it bears a label for the b and t commands to branch to.
[1addr]=
Place the current line number on the standard output as a line with its own line number.
[0addr]
An empty command; ignored.
[0addr]#
If a # appears as the first character on any line of a script file, that entire line is ignored (treated as a comment), with the single exception that if the first line of the script file begins with #n, the default output is suppressed (as when the -n option is specified on the command line).

For more information, see:

Examples:

In the file myfile, find and output only those lines containing the string "tom":

    sed -n -e "/tom/p" myfile

In the file myfile, replace all occurrences of the string beneath with the string below, and output to the file newfile:

    sed -e "s/beneath/below/" myfile >newfile

Files:

All files are text files. The script_files named by the -f option consist of editing commands, one per line. Any number of additional text input files may be specified by the r command for insertion of unedited data to the standard output at points predetermined by editing rules. A maximum of 10 additional output files may be specified through the use of the w command in the script.

Exit status:

0
Successful completion.
>0
An error occurred.

Errors:

If one or more of the input files (this doesn't include script files) can't be opened for reading, sed continues to process the remaining files.

Contributing author:

GNU