< Day Day Up > |
5.6. Commands and OptionsSed commands tell sed how to process each line of input specified by an address. If an address is not given, sed processes every line of input. (The % is the csh prompt.) See Table 5.1 for a list of sed commands and what they do, and see Table 5.2 for a list of options and how they control sed's behavior.
When multiple commands are used or addresses need to be nested within a range of addresses, the commands are enclosed in curly braces and each command is either on a separate line or terminated with semicolons. The exclamation point (!) can be used to negate a command. For example, sed '/Tom/d' file tells sed to delete all lines containing the pattern Tom, whereas sed '/Tom/!d' file (sh, ksh, bash) sed '/Tom/\!d' file (csh, tcsh) tells sed to delete lines not containing Tom. The sed options are –e, –f, and, –n. The –e is used for multiple edits at the command line, the –f precedes a sed script filename, and the –n suppresses printing output. 5.6.1 How to Modify a File with sedSed is a nondestructive editor. It will display the edits you make on your screen, but it will not change the file you are editing. To really reflect the edits in the file, you must redirect the output to another file, and then rename the orginal file. Example 5.5.1 % sed '1,3d' filex > temp 2 % mv temp filex EXPLANATION
5.6.2 GNU sed OptionsExample 5.6 lists additional options provided by GNU sed, and how they control sed's behavior. With the –h option, sed displays a list of its command-line options and a short description of what each one does. Example 5.6.
% sed -h
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--help display this help and exit
-V, --version output version information and exit
EXPLANATION If an –e, – –expression, –f, or – –file option is not given, then the first nonoption argument is taken as a sed script to be interpreted. All remaining arguments are names of input files; if no input files are specified, then the standard input is read. |
< Day Day Up > |