SED(1)

NAME
sed - stream-oriented editor

SYNOPSIS

sed [ -hnV ] [ -e script ] [ -f script-file ] [ --help ] [ --quiet ] [ --silent ] [ --version ] [ --expression=script ] [ --file=script-file ] [ file ... ]

DESCRIPTION

sed reads the specified files or the standard input if no files are specified, makes editing changes according to a list of commands, and writes the results to the standard output.

OPTIONS

-h, --help
    Print a usage message on standard output and exit successfully.

-n, --quiet, --silent
    Suppress the default output. sed only displays lines explicitly specified for output with the p command or the p flag of the s command. The default behavior is to echo each line of input, after edits, to the standard output.

-V, --version
    Print the version number on the standard output and exit successfully.

-e script, --expression=script
    Append one or more commands specified in the string script to the list of commands. If there is just one -e option and no -f options, the -e flag may be omitted.

-f script-file, --file=script-file
    Append the editing commands from script-file to the list of commands.

Multiple -e and -f commands may be specified. Scripts are added to the list of commands to execute in the order specified, regardless of their origin.

USAGE

  Operation

sed operates as follows:

+o  Each line of input, not including its terminating newline character, is successively copied into a pattern space (a temporary buffer).

+o  All editing commands whose addresses match that pattern space are sequentially applied to the pattern space.

+o  When reaching the end of the command list, the pattern space is written to the standard output (except under -n) with an appended newline.

+o  The pattern space is cleared and the process is repeated for each line in the input.

With sed, original input files remain unchanged since editing commands only modify a copy of the input.

Some sed commands use a hold space to save all or part of the pattern space for later retrieval.

  Command Syntax

A sed script consists of commands with the general form:

    [address[,address]][!]command[arguments]

Typically, there is only one command per line, but commands may also be concatenated on a single line by semicolons.

Whitespace characters may be inserted before the first address and the command portions of the script command.

  Addresses
A sed command, as indicated above, can specify zero, one, or two addresses. An address can be:

+o  A line number, represented in decimal. The internal line number count maintained by sed is cumulative across input files and is not reset for each input file.

+o  A pattern which is a regular expression, represented by \cpatternc, where c is any character except backslash (`\') or newline. In the address `\xabc\xdefx', the second `x' stands for itself, so the regular expression is `abcxdef'. However, the preferred (and equivalent) method to construct a regular expression is to enclose the pattern in slashes — `/pattern/'. Additionally, \n can be used to match any newline in the pattern space, except for the final newline character.

+o  A $ character which addresses the last line of input.

+o  GNU sed also implements a new type of address. The address has form n~m, which matches any line where the line number modulo m is equal to n modulo m. If m is 0 or missing, then 1 is used in its place. This feature is not specified by POSIX.

The following rules apply to addressed commands:

+o  A command line with no address selects each input line.

+o  A command line with one address selects any line matching the address. Several commands accept only one address: =, a, i, r, and q.

+o  A command line with two comma-separated addresses selects the first matching line and all following lines up to and including the line matching the second address. If the second address starts before or is the same line as the first address, then only the first line is selected.

+o  An address followed by `!' selects all lines that do not match the address.

  Regular Expressions
Regular expressions are patterns used in selecting text. For example, the sed command

    /string/p

prints all lines containing string. In addition to a specifying string literals, regular expressions can represent classes of strings. Strings thus represented are said to be matched by the corresponding regular expression. If it is possible for a regular expression to match several strings in a line, then the left-most longest match is the one selected. The following symbols are used in constructing search patterns:

    The null regular expression is equivalent to the last regular expression used.

c   Any character c not listed below, including `{', '}', `(', `)', `<', `>', `|', and `+' matches itself.

\c  Any backslash-escaped character c, except for `{', '}', `(', `)', `<', `>', `|', and `+' matches itself.

.   Matches any single character except newline.

[char-class]
    Matches any single character, other than newline, in char-class. To include a `]' in char-class, it must be the first character. A range of characters may be specified by separating the end characters of the range with a `-', e.g., `a-z' specifies the lower case characters. The following literal expressions can also be used in char-class to specify sets of characters:

    [:alnum:] [:cntrl:] [:lower:] [:space:]
    [:alpha:] [:digit:] [:print:] [:upper:]
    [:blank:] [:graph:] [:punct:] [:xdigit:]

    If `-' appears as the first or last character of char-class, then it matches itself. All other characters in char-class match themselves.

[^char-class]
    Matches any single character, other than newline, not in char-class. char-class is defined as above.

^   If `^' is the first character of a regular expression, then it anchors the regular expression to the beginning of a line. Otherwise, it matches itself.

$   If `$' is the last character of a regular expression, it anchors the regular expression to the end of a line. Otherwise, it matches itself.

\<

\>  Anchors the single character regular expression or subexpression immediately following it to the beginning (\<) or ending (\>) of a word, i.e., in ASCII, a maximal string of alphanumeric characters, including the underscore (_).

\(re\)
    Defines a (possibly null) subexpression re. Subexpressions may be nested. A subsequent backreference of the form `\n', where n is a number in the range [1,9], expands to the text matched by the nth subexpression. For example, the regular expression `\(a.c\)\1' matches the string `abcabc', but not `abcadc'. Subexpressions are ordered relative to their left delimiter.

*   Matches the single character regular expression or subexpression immediately preceding it zero or more times. If `*' is the first character of a regular expression or subexpression, then it matches itself. The `*' operator sometimes yields unexpected results. For example, the regular expression `b*' matches the beginning of the string `abbb' (as opposed to the substring `bbb'), since a null match is the only left-most match.

\+  Matches the single character regular expression or subexpression immediately preceding it one or more times.

\|  Match the regular expression or subexpression specified before or after it.

\{n,m\} or \{n,\} or \{n\}
    Matches the single character regular expression or subexpression immediately preceding it at least n and at most m times. If m is omitted, then it matches at least n times. If the comma is also omitted, then it matches exactly n times.

(\group\)
    Match the enclosed group of regular expressions.

The following characters only have special meaning when used in replacement patterns:

\   Escape the following character.

\n  Matches the nth pattern previously saved by `\(' and `\)', where n is a number from 0 to 9. Previously saved patterns are counted from the left-most position on the line.

&   Prints the entire search pattern when used in a replacement string.

  Comments
If the first nonwhite character in a line is a # (pound sign), sed treats that line as a comment, and ignores it. If, however, the first such line is of the form:

    #n

sed runs as if the -n flag were specified.

  Grouping Commands
Braces (`{', `}') can be used to nest one address within another or to apply multiple commands to the same address.
	[address][,address]{
	command 1
	command 2
	...
	}
The opening `{' must end a line and the closing `}' must be on a line by itself.

  Commands
The maximum number of permissible addresses for each command is indicated in parentheses in the list below.

An argument denoted text consists of one or more lines of text. If text is longer than one line in length, then any newline characters must be hidden by preceding them with a backslash (`\').

An argument denoted read-filename or write-filename must terminate the command line and must be preceded by exactly one space. Each write-filename is created before processing begins.

(0) An empty command is ignored.

(0)#comment
    The line is a comment and is ignored by sed. If, however, the first such line in a script is of the form `#n', then sed behaves as if the -n flag had been specified.

(0): label
    Affix label to a line in the script for a transfer of control by b or t commands.

(1)=
    Write the current line number on the standard output as a line.

(1)a\
text
    Append text following each line matched by the address on the standard output before reading the next input line.

(2)b label
    Unconditionally transfer control to the `:' command bearing the label. If no label is specified, then branch to the end of the script; no more commands are executed on the current pattern space.

(2)c\
text
    Change the pattern space by replacing the selected pattern with text. When multiple lines are specified, all lines in the pattern space are replaced with a single copy of text. The end result is that the pattern space is deleted and no further editing commands can be applied to it.

(2)d
    Delete the pattern space, preventing the line from being passed to the standard output, and start the next cycle.

(2)D
    Delete the initial segment of the pattern space through the first newline and start the next cycle.

(2)g
    Replace the contents of the pattern space by the contents of the hold space.

(2)G
    Append a newline character followed by the contents of the hold space to the pattern space.

(2)h
    Replace the contents of the hold space by the contents of the pattern space.

(2)H
    Append a newline character followed by the contents of the pattern space to the hold space.

(1)i\
text
    Insert text by writing it to the standard output.

(2)l
    Write the pattern space to standard output in a visually unambiguous form. Non-printing characters are displayed as either 3-digit octal values, preceded by a `\', or as one of the following character constant escape sequences:

    	\\	backslash
    	\a	alert
    	\b	backspace
    	\f	form-feed
    	\n	newline
    	\r	carriage-return
    	\t	tab
    	\v	vertical tab

    Long lines are folded, with the point of folding indicated by a backslash (`\') and a newline character. The end of every line is marked with a `$'.

(2)n
    Copy the pattern space to the standard output. Replace the pattern space with the next line of input.

(2)N
    Append the next line of input to the pattern space with an embedded newline. (The current line number changes.)

(2)p
    Print the pattern space to the standard output.

(2)P
    Copy the initial segment of the pattern space through the first newline to the standard output.

(1)q
    Quit by transferring control to the end of the script and do not start a new cycle. The pattern space is still written to the standard output.

(2)r read-filename
    Read the contents of read-filename. Place them on the output before reading the next input line.

(2)s/regular expression/replacement/flags
    Substitute the replacement string for instances of the regular expression in the pattern space. Any character may be used instead of `/'. For a fuller description, see the above section on replacement patterns. flags is zero or more of:

    n   Substitute for just the nth occurrence of the regular expression.

    g   Globally substitute for all non-overlapping instances of the regular expression rather than just the first one.

    p   Print the pattern space if a replacement was made.

    w write-filename
        Append the pattern space to write-filename if a replacement was made.

(2)t label
    Branch to the `:' 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.

(2)w write-filename
    Append the pattern space to write-filename.

(2)x
    Exchange the contents of the pattern and hold spaces.

(2)y/string1/string2/
    Replace all occurrences of characters in string1 with the corresponding character in string2. The lengths of string1 and string2 must be equal. Any character other than `' or newline can be used instead of slash to delimit the strings. Within string1 and string2, the delimiter itself can be used as a literal character if it is preceded by a backslash.

DIAGNOSTICS

Command only uses one address
    A command that takes one address had two addresses specified.

Command doesn't take any addresses
    A command that takes no addresses had an address specified.

Extra characters after command
    A command had extra text after the end.

Unexpected End-of-file
    The end of a script was reached before it should have been. This usually occurs when a command is started, but not finished.

No previous regular expression
    A meta-character calling for a previous regular expression before any regular expressions were used.

Missing command
    An address was not followed by a command.

Unknown command
    A command was not one of the ones recognized by sed.

Unexpected ','
    A command had a spurious comma after an address.

Multiple '!'s
    More than one `!' (exclamation point) was used in a command.

Unexpected '}'
    A `}' character was given in a command without a preceding `{'.

Unexpected '{'
    A `{' character was given in a command without a following `}'.

} doesn't want any addresses
    `}' should be alone on a line.

: doesn't want any addresses
    The `:' command should not be preceded by an address.

Unterminated `s' command
    The replacement field of the `s' command should be completed with a `/' character.

multiple 'p' options to 's' command
    The `p' option was given more than once in an `s' command.

multiple 'g' options to 's' command
    The `g' option was given more than once in an `s' command.

multiple number options to 's' command
    More than one number option was given to an `s' command.

Unknown option to 's'
    An unknown option was used for the `s' command. Maybe you shouldn't do that.

strings for y command are different lengths
    There should be a one-to-one mapping between strings for the `y' command.

missing ' ' before filename
    There was no space between a `r', `w', or `s///w' command, and the filename specified for that command.

Hopelessly evil compiled in limit on number of open files.
    re-compile sed. An attempt was made to open too many files, no matter how you look at it.

SEE ALSO
awk(1), ed(1), grep(1), perl(1), regex(3)

HISTORY
A sed command appeared in Version 7 AT&T UNIX.

STANDARDS
GNU sed is expected to be a superset of the IEEE Std1003.2 (POSIX) specification.

CAVEATS
GNU sed uses the POSIX basic regular expression syntax. According to the standard, the meaning of some escape sequences is undefined in this syntax; notably `\|' and `\+'. As in all GNU programs that use POSIX basic regular expressions, sed interprets these escape sequences as meta-characters. So, `x\+' matches one or more occurrences of `x'. `abc\|def' matches either `abc' or `def'. This syntax may cause problems when running scripts written for other versions of sed. Some sed programs have been written with the assumption that `\|' and `\+' match the literal characters `|' and `+'. Such scripts must be modified by removing the spurious backslashes if they are to be used with GNU sed.

BUGS
It has long been noted that GNU sed is much slower than other implementations. The current bottleneck is the way sed reads and writes data files. It should read large blocks at a time (or even map files, where that is supported). When possible, it should avoid copying it's input from one place in memory to another. Patches to make it do those things are welcome!