1SED(1) General Commands Manual SED(1)
2
3
4
6 sed - stream editor
7
9 sed [ -n ] [ -e script ] [ -f sfile ] [ file ] ...
10
12 Sed copies the named files (standard input default) to the standard
13 output, edited according to a script of commands. The -f option causes
14 the script to be taken from file sfile; these options accumulate. If
15 there is just one -e option and no -f's, the flag -e may be omitted.
16 The -n option suppresses the default output.
17
18 A script consists of editing commands, one per line, of the following
19 form:
20
21 [address [, address] ] function [arguments]
22
23 In normal operation sed cyclically copies a line of input into a pat‐
24 tern space (unless there is something left after a `D' command),
25 applies in sequence all commands whose addresses select that pattern
26 space, and at the end of the script copies the pattern space to the
27 standard output (except under -n) and deletes the pattern space.
28
29 An address is either a decimal number that counts input lines cumula‐
30 tively across files, a `$' that addresses the last line of input, or a
31 context address, `/regular expression/', in the style of ed(1) modified
32 thus:
33
34 The escape sequence `\n' matches a newline embedded in the pat‐
35 tern space.
36
37 A command line with no addresses selects every pattern space.
38
39 A command line with one address selects each pattern space that matches
40 the address.
41
42 A command line with two addresses selects the inclusive range from the
43 first pattern space that matches the first address through the next
44 pattern space that matches the second. (If the second address is a
45 number less than or equal to the line number first selected, only one
46 line is selected.) Thereafter the process is repeated, looking again
47 for the first address.
48
49 Editing commands can be applied only to non-selected pattern spaces by
50 use of the negation function `!' (below).
51
52 In the following list of functions the maximum number of permissible
53 addresses for each function is indicated in parentheses.
54
55 An argument denoted text consists of one or more lines, all but the
56 last of which end with `\' to hide the newline. Backslashes in text
57 are treated like backslashes in the replacement string of an `s' com‐
58 mand, and may be used to protect initial blanks and tabs against the
59 stripping that is done on every script line.
60
61 An argument denoted rfile or wfile must terminate the command line and
62 must be preceded by exactly one blank. Each wfile is created before
63 processing begins. There can be at most 10 distinct wfile arguments.
64
65 (1)a\
66 text
67 Append. Place text on the output before reading the next input
68 line.
69
70 (2)b label
71 Branch to the `:' command bearing the label. If label is empty,
72 branch to the end of the script.
73
74 (2)c\
75 text
76 Change. Delete the pattern space. With 0 or 1 address or at
77 the end of a 2-address range, place text on the output. Start
78 the next cycle.
79
80 (2)d Delete the pattern space. Start the next cycle.
81
82 (2)D Delete the initial segment of the pattern space through the
83 first newline. Start the next cycle.
84
85 (2)g Replace the contents of the pattern space by the contents of the
86 hold space.
87
88 (2)G Append the contents of the hold space to the pattern space.
89
90 (2)h Replace the contents of the hold space by the contents of the
91 pattern space.
92
93 (2)H Append the contents of the pattern space to the hold space.
94
95 (1)i\
96 text Insert. Place text on the standard output.
97
98 (2)l List the pattern space on the standard output in an unambiguous
99 form. Non-printing characters are spelled in two digit ascii,
100 and long lines are folded.
101
102 (2)n Copy the pattern space to the standard output. Replace the pat‐
103 tern space with the next line of input.
104
105 (2)N Append the next line of input to the pattern space with an
106 embedded newline. (The current line number changes.)
107
108 (2)p Print. Copy the pattern space to the standard output.
109
110 (2)P Copy the initial segment of the pattern space through the first
111 newline to the standard output.
112
113 (1)q Quit. Branch to the end of the script. Do not start a new
114 cycle.
115
116 (2)r rfile
117 Read the contents of rfile. Place them on the output before
118 reading the next input line.
119
120 (2)s/regular expression/replacement/flags
121 Substitute the replacement string for instances of the regular
122 expression in the pattern space. Any character may be used
123 instead of `/'. For a fuller description see ed(1). Flags is
124 zero or more of
125
126 g Global. Substitute for all nonoverlapping instances of
127 the regular expression rather than just the first one.
128
129 p Print the pattern space if a replacement was made.
130
131 w wfile
132 Write. Append the pattern space to wfile if a replace‐
133 ment was made.
134
135 (2)t label
136 Test. Branch to the `:' command bearing the label if any sub‐
137 stitutions have been made since the most recent reading of an
138 input line or execution of a `t'. If label is empty, branch to
139 the end of the script.
140
141 (2)w wfile
142 Write. Append the pattern space to wfile.
143
144 (2)x Exchange the contents of the pattern and hold spaces.
145
146 (2)y/string1/string2/
147 Transform. Replace all occurrences of characters in string1
148 with the corresponding character in string2. The lengths of
149 string1 and string2 must be equal.
150
151 (2)! function
152 Don't. Apply the function (or group, if function is `{') only
153 to lines not selected by the address(es).
154
155 (0): label
156 This command does nothing; it bears a label for `b' and `t' com‐
157 mands to branch to.
158
159 (1)= Place the current line number on the standard output as a line.
160
161 (2){ Execute the following commands through a matching `}' only when
162 the pattern space is selected.
163
164 (0) An empty command is ignored.
165
167 ed(1), grep(1), awk(1)
168
169
170
171 SED(1)