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.
% 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.
Command | Function |
---|---|
a | Appends one or more lines of text to the current line. |
b label | Branches to the : command bearing the label. Without a label, branches to the end of the script. |
c | Changes (replaces) text in the current line with new text. |
d | Deletes a line(s) from the pattern space. |
D | Deletes the first line of the pattern space. |
i | Inserts text above the current line. |
h | Copies the contents of the pattern space to a holding buffer. |
H | Appends the contents of the pattern space to a holding buffer. |
g | Gets what is in the holding buffer and copies it into the pattern buffer, overwriting what was there. |
G | Gets what is in the holding buffer and copies it into the pattern buffer, appending to what was there. |
l | Lists nonprinting characters. |
n | Reads the next input line and starts processing the new line with the next command rather than the first command. |
N | Appends the next line of input to the current pattern space, inserting an embedded newline between the two. Changes the current line number. |
p | Prints lines in the pattern space. |
P | Prints the first line from the pattern space. |
q | Quits or exits sed. |
r file | Reads lines from file. |
t label | Branch-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 label | Branch-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 file | Writes and appends the pattern space to file. |
W file | Writes 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. |
# comment | The comment extends until the next newline (or the end of an -e script expression). |
Substitution Flags | |
g | Globally substitutes on a line. |
p | Prints lines. |
w | Writes lines out to a file. |
x | Exchanges contents of the holding buffer with the pattern space. |
y | Translates one character to another (cannot use regular expression metacharacters with y). |
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.
3.133.158.32