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