manpagez: man pages & more
info sed
Home | html | info | man

File: sed.info,  Node: Common Commands,  Next: Other Commands,  Prev: The "s" Command,  Up: sed scripts

3.4 Often-Used Commands
=======================

If you use ‘sed’ at all, you will quite likely want to know these
commands.

‘#’
     [No addresses allowed.]

     The ‘#’ character 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 conforming) 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’.

‘q [EXIT-CODE]’
     Exit ‘sed’ without processing any more commands or input.

     Example: stop after printing the second line:
          $ seq 3 | sed 2q
          1
          2

     This command accepts only one address.  Note that the current
     pattern space is printed if auto-print is not disabled with the
     ‘-n’ options.  The ability to return an exit code from the ‘sed’
     script is a GNU ‘sed’ extension.

     See also the GNU ‘sed’ extension ‘Q’ command which quits silently
     without printing the current pattern space.

‘d’
     Delete the pattern space; immediately start next cycle.

     Example: delete the second input line:
          $ seq 3 | sed 2d
          1
          3

‘p’
     Print out the pattern space (to the standard output).  This command
     is usually only used in conjunction with the ‘-n’ command-line
     option.

     Example: print only the second input line:
          $ seq 3 | sed -n 2p
          2

‘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.

     This command is useful to skip lines (e.g.  process every Nth
     line).

     Example: perform substitution on every 3rd line (i.e.  two ‘n’
     commands skip two lines):
          $ seq 6 | sed 'n;n;s/./x/'
          1
          2
          x
          4
          5
          x

     GNU ‘sed’ provides an extension address syntax of FIRST~STEP to
     achieve the same result:

          $ seq 6 | sed '0~3s/./x/'
          1
          2
          x
          4
          5
          x

‘{ COMMANDS }’
     A group of commands may be enclosed between ‘{’ and ‘}’ characters.
     This is particularly useful when you want a group of commands to be
     triggered by a single address (or address-range) match.

     Example: perform substitution then print the second input line:
          $ seq 3 | sed -n '2{s/2/X/ ; p}'
          X

© manpagez.com 2000-2026
Individual documents may contain additional copyright information.