4.5. Commands and Options

Sed 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. If a sed command is preceded by an exclamation point (!), the negation operator, the command affects every line not selected by the address(es) The % is the shell prompt. See Table 4.1 for a list of sed commands and what they do, and see Table 4.2 for a list of options 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 4.3.
% 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.

Table 4.1. sed Commands
CommandFunction
aAppends one or more lines of text to the current line.
b labelBranches to the : command bearing the label. Without a label, branches to the end of the script.
cChanges (replaces) text in the current line with new text.
dDeletes a line(s) from the pattern space.
DDeletes the first line of the pattern space.
iInserts text above the current line.
hCopies the contents of the pattern space to a holding buffer.
HAppends the contents of the pattern space to a holding buffer.
gGets what is in the holding buffer and copies it into the pattern buffer, overwriting what was there.
GGets what is in the holding buffer and copies it into the pattern buffer, appending to what was there.
lLists nonprinting characters.
nReads the next input line and starts processing the new line with the next command rather than the first command.
NAppends the next line of input to the current pattern space, inserting an embedded newline between the two. Changes the current line number.
pPrints lines in the pattern space.
PPrints the first line from the pattern space.
qQuits or exits sed.
r fileReads lines from file.
t labelBranch-if-test, i.e., if any substitutions have been made since last input line or t or T command, branches to the : command with the label or to the end of file if no label.
T labelBranch-on-error, i.e., if no substitutions have succeeded since last input line or t or T command, branches to the : command with the label and to the end of file if no label.
w fileWrites and appends the pattern space to file.
W fileWrites and appends first line of the pattern space to file.
!Applies the command to all lines except the selected ones.
s/re/string/Substitutes regular expression, re, with string.
=Prints the current line number.
# commentThe comment extends until the next newline (or the end of an -e script expression).
Substitution Flags
gGlobally substitutes on a line.
pPrints lines.
wWrites lines out to a file.
xExchanges contents of the holding buffer with the pattern space.
yTranslates one character to another (cannot use regular expression metacharacters with y).

Table 4.2. sed Options
OptionsFunction
-e command, --expression=commandAllows multiple edits.Same as above.
-h, --helpPrints a message summarizing the command line options and the address for reporting bugs.
-n, --quiet, --silentSuppresses default output.
-f, --file=script-filePrecedes a sed script filename. Same as above (script-file is file containing sed commands.)
-V, --versionPrints version and copyright information.

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
				

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.55.198