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