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