1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
SED(1) User Commands SED(1)
NAME
sed - stream editor for filtering and transforming text
SYNOPSIS
sed.exe [OPTION]... {script-only-if-no-other-script}
[input-file]...
DESCRIPTION
Sed is a stream editor. A stream editor is used to per-
form basic text transformations on an input stream (a
file or input from a pipeline). While in some ways sim-
ilar to an editor which permits scripted edits (such as
ed), sed works by making only one pass over the
input(s), and is consequently more efficient. But it is
sed's ability to filter text in a pipeline which partic-
ularly distinguishes it from other types of editors.
-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
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension
supplied)
-b, --binary
open files in binary mode (CR+LFs are not pro-
cessed specially)
-c, --copy
use copy instead of rename when shuffling files
in -i mode (avoids change of input file owner-
ship)
-l N, --line-length=N
specify the desired line-wrap length for the `l'
command
--posix
disable all GNU extensions.
-r, --regexp-extended
use extended regular expressions in the script.
-s, --separate
consider files as separate rather than as a sin-
gle continuous long stream.
-u, --unbuffered
load minimal amounts of data from the input files
and flush the output buffers more often
--help
display this help and exit
--version
output version information and exit
If no -e, --expression, -f, or --file option is given,
then the first non-option argument is taken as the sed
script to interpret. All remaining arguments are names
of input files; if no input files are specified, then
the standard input is read.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software:
<http://www.gnu.org/gethelp/>. E-mail bug reports to:
<bug-gnu-utils@gnu.org>. Be sure to include the word
``sed'' somewhere in the ``Subject:'' field.
COMMAND SYNOPSIS
This is just a brief synopsis of sed commands to serve
as a reminder to those who already know sed; other docu-
mentation (such as the texinfo document) must be con-
sulted for fuller descriptions.
Zero-address ``commands''
: label
Label for b and t commands.
#comment
The comment extends until the next newline (or
the end of a -e script fragment).
} The closing bracket of a { } block.
Zero- or One- address commands
= Print the current line number.
a \
text Append text, which has each embedded newline pre-
ceded by a backslash.
i \
text Insert text, which has each embedded newline pre-
ceded by a backslash.
q [exit-code]
Immediately quit the sed script without process-
ing any more input, except that if auto-print is
not disabled the current pattern space will be
printed. The exit code argument is a GNU exten-
sion.
Q [exit-code]
Immediately quit the sed script without process-
ing any more input. This is a GNU extension.
r filename
Append text read from filename.
R filename
Append a line read from filename. Each invoca-
tion of the command reads a line from the file.
This is a GNU extension.
Commands which accept address ranges
{ Begin a block of commands (end with a }).
b label
Branch to label; if label is omitted, branch to
end of script.
t label
If a s/// has done a successful substitution
since the last input line was read and since the
last t or T command, then branch to label; if
label is omitted, branch to end of script.
T label
If no s/// has done a successful substitution
since the last input line was read and since the
last t or T command, then branch to label; if
label is omitted, branch to end of script. This
is a GNU extension.
c \
text Replace the selected lines with text, which has
each embedded newline preceded by a backslash.
d Delete pattern space. Start next cycle.
D Delete up to the first embedded newline in the
pattern space. Start next cycle, but skip read-
ing from the input if there is still data in the
pattern space.
h H Copy/append pattern space to hold space.
g G Copy/append hold space to pattern space.
x Exchange the contents of the hold and pattern
spaces.
l List out the current line in a ``visually unam-
biguous'' form.
l width
List out the current line in a ``visually unam-
biguous'' form, breaking it at width characters.
This is a GNU extension.
n N Read/append the next line of input into the pat-
tern space.
p Print the current pattern space.
P Print up to the first embedded newline of the
current pattern space.
s/regexp/replacement/
Attempt to match regexp against the pattern
space. If successful, replace that portion
matched with replacement. The replacement may
contain the special character & to refer to that
portion of the pattern space which matched, and
the special escapes \1 through \9 to refer to the
corresponding matching sub-expressions in the
regexp.
w filename
Write the current pattern space to filename.
W filename
Write the first line of the current pattern space
to filename. This is a GNU extension.
y/source/dest/
Transliterate the characters in the pattern space
which appear in source to the corresponding char-
acter in dest.
Addresses
Sed commands can be given with no addresses, in which
case the command will be executed for all input lines;
with one address, in which case the command will only be
executed for input lines which match that address; or
with two addresses, in which case the command will be
executed for all input lines which match the inclusive
range of lines starting from the first address and con-
tinuing to the second address. Three things to note
about address ranges: the syntax is addr1,addr2 (i.e.,
the addresses are separated by a comma); the line which
addr1 matched will always be accepted, even if addr2
selects an earlier line; and if addr2 is a regexp, it
will not be tested against the line that addr1 matched.
After the address (or address-range), and before the
command, a ! may be inserted, which specifies that the
command shall only be executed if the address (or
address-range) does not match.
The following address types are supported:
number Match only the specified line number.
first~step
Match every step'th line starting with line
first. For example, ``sed -n 1~2p'' will print
all the odd-numbered lines in the input stream,
and the address 2~5 will match every fifth line,
starting with the second. first can be zero; in
this case, sed operates as if it were equal to
step. (This is an extension.)
$ Match the last line.
/regexp/
Match lines matching the regular expression reg-
exp.
\cregexpc
Match lines matching the regular expression reg-
exp. The c may be any character.
GNU sed also supports some special 2-address forms:
0,addr2
Start out in "matched first address" state, until
addr2 is found. This is similar to 1,addr2,
except that if addr2 matches the very first line
of input the 0,addr2 form will be at the end of
its range, whereas the 1,addr2 form will still be
at the beginning of its range. This works only
when addr2 is a regular expression.
addr1,+N
Will match addr1 and the N lines following addr1.
addr1,~N
Will match addr1 and the lines following addr1
until the next line whose input line number is a
multiple of N.
REGULAR EXPRESSIONS
POSIX.2 BREs should be supported, but they aren't com-
pletely because of performance problems. The \n
sequence in a regular expression matches the newline
character, and similarly for \a, \t, and other
sequences.
BUGS
E-mail bug reports to bonzini@gnu.org. Be sure to
include the word ``sed'' somewhere in the ``Subject:''
field. Also, please include the output of ``sed --ver-
sion'' in the body of your report if at all possible.
COPYRIGHT
Copyright � 2009 Free Software Foundation, Inc.
This is free software; see the source for copying condi-
tions. There is NO warranty; not even for MERCHANTABIL-
ITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent
permitted by law.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software:
<http://www.gnu.org/gethelp/>. E-mail bug reports to:
<bug-gnu-utils@gnu.org>. Be sure to include the word
``sed'' somewhere in the ``Subject:'' field.
SEE ALSO
awk(1), ed(1), grep(1), tr(1), perlre(1), sed.info, any
of various books on sed, the sed FAQ
(http://sed.sf.net/grabbag/tutorials/sedfaq.txt),
http://sed.sf.net/grabbag/.
The full documentation for sed is maintained as a Tex-
info manual. If the info and sed programs are properly
installed at your site, the command
info sed
should give you access to the complete manual.
sed version 4.2.1 June 2009 SED(1)
|