GNU Info

Info Node: (sed.info)Common Commands

(sed.info)Common Commands


Next: Other Commands Prev: Data Spaces Up: sed Programs
Enter node , (file) or (file)node

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.


automatically generated by info2www version 1.2.2.9