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