Go to the first, previous, next, last section, table of contents.


Often used commands

If you use SED at all, you will quite likely want to know these commands.

`#'
[No addresses allowed.] The # "command" begins a comment; the comment continues until the next newline. If you are concerned about portability, be aware that some implementations of SED (which are not POSIX.2 conformant) may only support a single one-line comment, and then only when the very first character of the script is a #. Warning: if the first two characters of the SED script are #n, then the -n (no-autoprint) option is forced. If you want to put a comment in the first line of your script and that comment begins with the letter `n' and you do not want this behavior, then be sure to either use a capital `N', or place at least one space before the `n'.
`s/regexp/replacement/flags'
(The / characters may be uniformly replaced by any other single character within any given s command.) The / character (or whatever other character is used in its stead) can appear in the regexp or replacement only if it is preceded by a \ character. Also newlines may appear in the regexp using the two character sequence \n. The s command attempts to match the pattern space against the supplied regexp. If the match is successful, then that portion of the pattern space which was matched is replaced with replacement. The replacement can contain \n (n being a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between the nth \( and its matching \). Also, the replacement can contain unescaped & characters which will reference the whole matched portion of the pattern space. To include a literal \, &, or newline in the final replacement, be sure to precede the desired \, &, or newline in the replacement with a \. The s command can be followed with zero or more of the following flags:
`g'
Apply the replacement to all matches to the regexp, not just the first.
`p'
If the substitution was made, then print the new pattern space.
`number'
Only replace the numberth match of the regexp.
`w file-name'
If the substitution was made, then write out the result to the named file.
`I'
(This is a GNU extension.) Match regexp in a case-insensitive manner.
`q'
[At most one address allowed.] Exit SED without processing any more commands or input. Note that the current pattern space is printed if auto-print is not disabled.
`d'
Delete the pattern space; immediately start next cycle.
`p'
Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option. Note: some implementations of SED, such as this one, will double-print lines when auto-print is not disabled and the p command is given. Other implementations will only print the line once. Both ways conform with the POSIX.2 standard, and so neither way can be considered to be in error. Portable SED scripts should thus avoid relying on either behavior; either use the -n option and explicitly print what you want, or avoid use of the p command (and also the p flag to the s command).
`n'
If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then SED exits without processing any more commands.
`{ commands }'
A group of commands may be enclosed between { and } characters. (The } must appear in a zero-address command context.) This is particularly useful when you want a group of commands to be triggered by a single address (or address-range) match.


Go to the first, previous, next, last section, table of contents.