blob: decf69b27d6908a611a712969fd39aa532f55908 [file] [log] [blame]
Denis Vlasenko72cc9642007-11-13 17:13:31 +00001<html>
2
3<head><title>Command Summary for sed (sed & awk, Second Edition)</title>
4</head>
5
6<body>
7
8<h2>Command Summary for sed</h2>
9
10<dl>
11
12<dt><b>: </b> <b> :</b><em>label</em></dt>
13<dd>Label a line in the script for the transfer of control by
14<b>b</b> or <b>t</b>.
15<em>label</em> may contain up to seven characters.
16(The POSIX standard says that an implementation can allow longer
17labels if it wishes to. GNU sed allows labels to be of any length.)
18</p></dd>
19
20
21<dt><b>=</b> [<em>address</em>]<b>=</b></dt>
22<dd>Write to standard output the line number of addressed line.</p></dd>
23
24
25<dt><b>a</b> [<em>address</em>]<b>a\</b></dt>
26<dd><em>text</em></p>
27
28<p>Append <em>text</em>
29following each line matched by <em>address</em>. If
30<em>text</em> goes over more than one line, newlines
31must be "hidden" by preceding them with a backslash. The
32<em>text</em> will be terminated by the first
33newline that is not hidden in this way. The
34<em>text</em> is not available in the pattern space
35and subsequent commands cannot be applied to it. The results of this
36command are sent to standard output when the list of editing commands
37is finished, regardless of what happens to the current line in the
38pattern space.</p></dd>
39
40
41<dt><b>b</b> [<em>address1</em>[,<em>address2</em>]]<b>b</b>[<em>label</em>]</dt>
42<dd>Transfer control unconditionally (branch) to
43<b>:</b><em>label</em> elsewhere in
44script. That is, the command following the
45<em>label</em> is the next command applied to the
46current line. If no <em>label</em> is specified,
47control falls through to the end of the script, so no more commands
48are applied to the current line.</p></dd>
49
50
51<dt><b>c</b> [<em>address1</em>[,<em>address2</em>]]<b>c\</b></dt>
52<dd><em>text</em></p>
53
54<p>Replace (change) the lines selected by the address with
55<em>text</em>. When a range of lines is specified,
56all lines as a group are replaced by a single copy of
57<em>text</em>. The newline following each line of
58<em>text</em> must be escaped by a backslash, except
59the last line. The contents of the pattern space are, in effect,
60deleted and no subsequent editing commands can be applied to it (or to
61<em>text</em>).</p></dd>
62
63
64<dt><b>d</b> [<em>address1</em>[,<em>address2</em>]]<b>d</b></dt>
65<dd>Delete line(s) from pattern space. Thus, the line is not passed to standard
66output. A new line of input is read and editing resumes with first
67command in script.</p></dd>
68
69
70<dt><b>D</b> [<em>address1</em>[,<em>address2</em>]]<b>D</b></dt>
71<dd>Delete first part (up to embedded newline) of multiline pattern space created
72by <b>N</b> command and resume editing with first command in
73script. If this command empties the pattern space, then a new line
74of input is read, as if the <b>d</b> command had been executed.</p></dd>
75
76
77<dt><b>g</b> [<em>address1</em>[,<em>address2</em>]]<b>g</b></dt>
78<dd>Copy (get) contents of hold space (see <b>h</b> or
79<b>H</b> command) into the pattern space, wiping out
80previous contents.</p></dd>
81
82
83<dt><b>G</b> [<em>address1</em>[,<em>address2</em>]]<b>G</b></dt>
84<dd>Append newline followed by contents of hold space (see
85<b>h</b> or <b>H</b> command) to contents of
86the pattern space. If hold space is empty, a newline is still
87appended to the pattern space.</p></dd>
88
89
90<dt><b>h</b> [<em>address1</em>[,<em>address2</em>]]<b>h</b></dt>
91<dd>Copy pattern space into hold space, a special temporary buffer.
92Previous contents of hold space are wiped out.</p></dd>
93
94
95<dt><b>H</b> [<em>address1</em>[,<em>address2</em>]]<b>H</b></dt>
96<dd>Append newline and contents of pattern space to contents of the hold
97space. Even if hold space is empty, this command still appends the
98newline first.</p></dd>
99
100
101<dt><b>i</b> [<em>address1</em>]<b>i\</b></dt>
102<dd><em>text</em></p>
103
104<p>Insert <em>text</em> before each line matched by
105<em>address</em>. (See <b>a</b> for
106details on <em>text</em>.)</p></dd>
107
108
109<dt><b>l</b> [<em>address1</em>[,<em>address2</em>]]<b>l</b></dt>
110<dd>List the contents of the pattern space, showing nonprinting characters
111as ASCII codes. Long lines are wrapped.</p></dd>
112
113
114<dt><b>n</b> [<em>address1</em>[,<em>address2</em>]]<b>n</b></dt>
115<dd>Read next line of input into pattern space. Current line is sent to
116standard output. New line becomes current line and increments line
117counter. Control passes to command following <b>n</b>
118instead of resuming at the top of the script.</p></dd>
119
120
121<dt><b>N</b> [<em>address1</em>[,<em>address2</em>]]<b>N</b></dt>
122<dd>Append next input line to contents of pattern space; the new line is
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000123separated from the previous contents of the pattern space by a newline.
Denis Vlasenko72cc9642007-11-13 17:13:31 +0000124(This command is designed to allow pattern matches across two
125lines. Using \n to match the embedded newline, you can match
126patterns across multiple lines.)</p></dd>
127
128
129<dt><b>p</b> [<em>address1</em>[,<em>address2</em>]]<b>p</b></dt>
130<dd>Print the addressed line(s). Note that this can result in duplicate
131output unless default output is suppressed by using "#n" or
132the <span class="option">-n</span>
133
134command-line option. Typically used before commands that change flow
135control (<b>d</b>, <b>n</b>,
136<b>b</b>) and might prevent the current line from being
137output.</p></dd>
138
139
140<dt><b>P</b> [<em>address1</em>[,<em>address2</em>]]<b>P</b></dt>
141<dd>Print first part (up to embedded newline) of multiline pattern space
142created by <b>N</b> command. Same as <b>p</b>
143if <b>N</b> has not been applied to a line.</p></dd>
144
145
146<dt><b>q</b> [<em>address</em>]<b>q</b></dt>
147<dd>Quit when <em>address</em> is encountered. The
148addressed line is first written to output (if default output is not
149suppressed), along with any text appended to it by previous
150<b>a</b> or <b>r</b> commands.</p></dd>
151
152
153<dt><b>r</b> [<em>address</em>]<b>r</b> <em>file</em></dt>
154<dd>Read contents of <em>file</em> and append after the
155contents of the pattern space. Exactly one space must be put between
156<b>r</b> and the filename.</p></dd>
157
158
159<dt><b>s</b> [<em>address1</em>[,<em>address2</em>]]<b>s</b>/<em>pattern</em>/<em>replacement</em>/[<em>flags</em>]</dt>
160<dd>Substitute <em>replacement</em> for
161<em>pattern</em> on each addressed line. If pattern
162addresses are used, the pattern <b>//</b> represents the
163last pattern address specified. The following flags can be specified:</p>
164
165 <dl>
166
167 <dt><b>n</b></dt>
168 <dd>Replace <em>n</em>th instance of
169 /<em>pattern</em>/ on each addressed line.
170 <em>n</em> is any number in the range 1 to 512, and
171 the default is 1.</p></dd>
172
173 <dt><b>g</b></dt>
174 <dd>Replace all instances of /<em>pattern</em>/ on each
175 addressed line, not just the first instance.</p></dd>
176
177 <dt><b>p</b></dt>
178 <dd>Print the line if a successful substitution is done. If several
179 successful substitutions are done, multiple copies of the line will be
180 printed.</p></dd>
181
182 <dt><b>w</b> <em>file</em></dt>
183 <dd>Write the line to <em>file</em> if a replacement
184 was done. A maximum of 10 different <em>files</em> can be opened.</p></dd>
185
186 </dl>
187
188</dd>
189
190
191<dt><b>t</b> [<em>address1</em>[,<em>address2</em>]]<b>t </b>[<em>label</em>]</dt>
192<dd>Test if successful substitutions have been made on addressed lines,
193and if so, branch to line marked by :<em>label</em>.
194(See <b>b</b> and <b>:</b>.) If label is not
195specified, control falls through to bottom of script.</p></dd>
196
197
198<dt><b>w</b> [<em>address1</em>[,<em>address2</em>]]<b>w</b> <em>file</em></dt>
199<dd>Append contents of pattern space to <em>file</em>.
200This action occurs when the command is encountered rather than when
201the pattern space is output. Exactly one space must separate the
202<b>w</b> and the filename. A maximum of 10 different
203files can be opened in a script. This command will create the file if
204it does not exist; if the file exists, its contents will be
205overwritten each time the script is executed. Multiple write commands
206that direct output to the same file append to the end of the file.</p></dd>
207
208
209<dt><b>x</b> [<em>address1</em>[,<em>address2</em>]]<b>x</b></dt>
210<dd>Exchange contents of the pattern space with the contents of the hold
211space.</p></dd>
212
213
214<dt><b>y</b> [<em>address1</em>[,<em>address2</em>]]<b>y</b>/<em>abc</em>/<em>xyz</em>/</dt>
215<dd>Transform each character by position in string
216<em>abc</em> to its equivalent in string
217<em>xyz</em>.</p></dd>
218
219
220</dl>