www.delorie.com/gnu/docs/sed/sed_7.html   search  
 
Buy the book!


sed, a stream editor

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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

q [exit-code]
This command only accepts a single address.

Exit sed without processing any more commands or input. 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.

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 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. This is particularly useful when you want a group of commands to be triggered by a single address (or address-range) match.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright © 2003   by The Free Software Foundation     Updated Jun 2003  

Please take a moment to fill out this visitor survey
You can help support this site by visiting the advertisers that sponsor it! (only once each, though)