Updated: April 19, 2023 |
Stream editor (POSIX)
sed [options]... {script-only-if-no-other-script} [input-file]...
QNX Neutrino, Microsoft Windows
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.
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.
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:
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:
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 at the length specified by the -l or --line-length option.
For more information, see:
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
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.
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.
GNU