sed: Common Commands

1 
1 3.4 Often-Used Commands
1 =======================
1 
1 If you use 'sed' at all, you will quite likely want to know these
1 commands.
1 
1 '#'
1      [No addresses allowed.]
1 
1      The '#' character begins a comment; the comment continues until the
1      next newline.
1 
1      If you are concerned about portability, be aware that some
1      implementations of 'sed' (which are not POSIX conforming) may only
1      support a single one-line comment, and then only when the very
1      first character of the script is a '#'.
1 
1      Warning: if the first two characters of the 'sed' script are '#n',
1      then the '-n' (no-autoprint) option is forced.  If you want to put
1      a comment in the first line of your script and that comment begins
1      with the letter 'n' and you do not want this behavior, then be sure
1      to either use a capital 'N', or place at least one space before the
1      'n'.
1 
1 'q [EXIT-CODE]'
1      Exit 'sed' without processing any more commands or input.
1 
1      Example: stop after printing the second line:
1           $ seq 3 | sed 2q
1           1
1           2
1 
1      This command accepts only one address.  Note that the current
1      pattern space is printed if auto-print is not disabled with the
1      '-n' options.  The ability to return an exit code from the 'sed'
1      script is a GNU 'sed' extension.
1 
1      See also the GNU 'sed' extension 'Q' command which quits silently
1      without printing the current pattern space.
1 
1 'd'
1      Delete the pattern space; immediately start next cycle.
1 
1      Example: delete the second input line:
1           $ seq 3 | sed 2d
1           1
1           3
1 
1 'p'
1      Print out the pattern space (to the standard output).  This command
1      is usually only used in conjunction with the '-n' command-line
1      option.
1 
1      Example: print only the second input line:
1           $ seq 3 | sed -n 2p
1           2
1 
1 'n'
1      If auto-print is not disabled, print the pattern space, then,
1      regardless, replace the pattern space with the next line of input.
1      If there is no more input then 'sed' exits without processing any
1      more commands.
1 
1      This command is useful to skip lines (e.g.  process every Nth
1      line).
1 
1      Example: perform substitution on every 3rd line (i.e.  two 'n'
1      commands skip two lines):
1           $ seq 6 | sed 'n;n;s/./x/'
1           1
1           2
1           x
1           4
1           5
1           x
1 
1      GNU 'sed' provides an extension address syntax of FIRST~STEP to
1      achieve the same result:
1 
1           $ seq 6 | sed '0~3s/./x/'
1           1
1           2
1           x
1           4
1           5
1           x
1 
1 '{ COMMANDS }'
1      A group of commands may be enclosed between '{' and '}' characters.
1      This is particularly useful when you want a group of commands to be
1      triggered by a single address (or address-range) match.
1 
1      Example: perform substitution then print the second input line:
1           $ seq 3 | sed -n '2{s/2/X/ ; p}'
1           X
1