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