xref: /csrg-svn/usr.bin/sed/sed.1 (revision 55999)
1*55999Sbostic.\" Copyright (c) 1992 The Regents of the University of California.
2*55999Sbostic.\" All rights reserved.
3*55999Sbostic.\"
4*55999Sbostic.\" This code is derived from software contributed to Berkeley by
5*55999Sbostic.\" the Institute of Electrical and Electronics Engineers, Inc.
6*55999Sbostic.\"
7*55999Sbostic.\" %sccs.include.redist.roff%
8*55999Sbostic.\"
9*55999Sbostic.\"	@(#)sed.1	5.1 (Berkeley) 08/23/92
10*55999Sbostic.\"
11*55999Sbostic.Dd ""
12*55999Sbostic.Dt SED 1
13*55999Sbostic.Os
14*55999Sbostic.Sh NAME
15*55999Sbostic.Nm sed
16*55999Sbostic.Nd stream editor
17*55999Sbostic.Sh SYNOPSIS
18*55999Sbostic.Nm sed
19*55999Sbostic.Op Fl an
20*55999Sbostic.Ar command
21*55999Sbostic.Op Ar file ...
22*55999Sbostic.Nm sed
23*55999Sbostic.Op Fl an
24*55999Sbostic.Op Fl e Ar command
25*55999Sbostic.Op Fl f Ar command_file
26*55999Sbostic.Op Ar file ...
27*55999Sbostic.Sh DESCRIPTION
28*55999SbosticThe
29*55999Sbostic.Nm sed
30*55999Sbosticutility reads the specified files, or the standard input if no files
31*55999Sbosticare specified, modifying the input as specified by a list of commands.
32*55999SbosticThe input is then written to the standard output.
33*55999Sbostic.Pp
34*55999SbosticA single command may be specified as the first argument to
35*55999Sbostic.Nm sed .
36*55999SbosticMultiple commands may be specified by using the
37*55999Sbostic.Fl e
38*55999Sbosticor
39*55999Sbostic.Fl f
40*55999Sbosticoptions.
41*55999SbosticAll commands are applied to the input in the order they are specified
42*55999Sbosticregardless of their origin.
43*55999Sbostic.Pp
44*55999SbosticThe following options are available:
45*55999Sbostic.Bl -tag -width indent
46*55999Sbostic.It Fl a
47*55999SbosticThe files listed as parameters for the
48*55999Sbostic.Dq w
49*55999Sbosticfunctions are created (or truncated) before any processing begins,
50*55999Sbosticby default.
51*55999SbosticThe
52*55999Sbostic.Fl a
53*55999Sbosticoption causes
54*55999Sbostic.Nm sed
55*55999Sbosticto delay opening each file until a command containing the related
56*55999Sbostic.Dq w
57*55999Sbosticfunction is applied to a line of input.
58*55999Sbostic.It Fl e Ar command
59*55999SbosticAppend the editing commands specified by the
60*55999Sbostic.Ar command
61*55999Sbosticargument
62*55999Sbosticto the list of commands.
63*55999Sbostic.It Fl f Ar command_file
64*55999SbosticAppend the editing commands found in the file
65*55999Sbostic.Ar command_file
66*55999Sbosticto the list of commands.
67*55999SbosticThe editing commands should each be listed on a separate line.
68*55999Sbostic.It Fl n
69*55999SbosticBy default, each line of input is echoed to the standard output after
70*55999Sbosticall of the commands have been applied to it.
71*55999SbosticThe
72*55999Sbostic.Fl n
73*55999Sbosticoption suppresses this behavior.
74*55999Sbostic.El
75*55999Sbostic.Pp
76*55999SbosticThe form of a
77*55999Sbostic.Nm sed
78*55999Sbosticcommand is as follows:
79*55999Sbostic.sp
80*55999Sbostic.Dl [address[,address]]function[arguments]
81*55999Sbostic.sp
82*55999SbosticWhitespace may be inserted before the first address and the function
83*55999Sbosticportions of the command.
84*55999Sbostic.Pp
85*55999SbosticNormally,
86*55999Sbostic.Nm sed
87*55999Sbosticcyclically copies a line of input, not including its terminating newline
88*55999Sbosticcharacter, into a
89*55999Sbostic.Em "pattern space" ,
90*55999Sbostic(unless there is something left after a
91*55999Sbostic.Dq D
92*55999Sbosticfunction),
93*55999Sbosticapplies all of the commands with addresses that select that pattern space,
94*55999Sbosticcopies the pattern space to the standard output, appending a newline, and
95*55999Sbosticdeletes the pattern space.
96*55999Sbostic.Pp
97*55999SbosticSome of the functions use a
98*55999Sbostic.Em "hold space"
99*55999Sbosticto save all or part of the pattern space for subsequent retrieval.
100*55999Sbostic.Sh "Sed Addresses"
101*55999SbosticAn address is not required, but if specified must be a number (that counts
102*55999Sbosticinput lines
103*55999Sbosticcumulatively across input files), a dollar
104*55999Sbostic.Po
105*55999Sbostic.Dq $
106*55999Sbostic.Pc
107*55999Sbosticcharacter that addresses the last line of input, or a context address
108*55999Sbostic(which consists of a regular expression preceded and followed by a
109*55999Sbosticdelimiter.
110*55999Sbostic.Pp
111*55999SbosticA command line with no addresses selects every pattern space.
112*55999Sbostic.Pp
113*55999SbosticA command line with one address selects all of the pattern spaces
114*55999Sbosticthat match the address.
115*55999Sbostic.Pp
116*55999SbosticA command line with two addresses selects the inclusive range from
117*55999Sbosticthe first pattern space that matches the first address through the next
118*55999Sbosticpattern space that matches the second.
119*55999Sbostic(If the second address is a number less than or equal to the line number
120*55999Sbosticfirst selected, only that line is selected.)
121*55999SbosticStarting at the first line following the selected range,
122*55999Sbostic.Nm sed
123*55999Sbosticstarts looking again for the first address.
124*55999Sbostic.Pp
125*55999SbosticEditing commands can be applied to non-selected pattern spaces by use
126*55999Sbosticof the exclamation character
127*55999Sbostic.Po
128*55999Sbostic.Dq !
129*55999Sbostic.Pc
130*55999Sbosticfunction.
131*55999Sbostic.Sh "Sed Regular Expressions"
132*55999SbosticThe
133*55999Sbostic.Nm sed
134*55999Sbosticregular expressions are basic regular expressions (BRE's, see
135*55999Sbostic.Xr regex 3
136*55999Sbosticfor more information).
137*55999SbosticIn addition,
138*55999Sbostic.Nm sed
139*55999Sbostichas the following two additions to BRE's:
140*55999Sbostic.sp
141*55999Sbostic.Bl -enum -compact
142*55999Sbostic.It
143*55999SbosticIn a context address, any character other than a backslash
144*55999Sbostic.Po
145*55999Sbostic.Dq \e
146*55999Sbostic.Pc
147*55999Sbosticor newline character may be used to delimit the regular expression.
148*55999SbosticAlso, putting a backslash character before the delimiting character
149*55999Sbosticcauses the character to be treated literally.
150*55999SbosticFor example, in the context address \exabc\exdefx, the RE delimiter
151*55999Sbosticis an
152*55999Sbostic.Dq x
153*55999Sbosticand the second
154*55999Sbostic.Dq x
155*55999Sbosticstands for itself, so that the regular expression is
156*55999Sbostic.Dq abcxdef .
157*55999Sbostic.sp
158*55999Sbostic.It
159*55999SbosticThe escape sequence \en matches a newline character embedded in the
160*55999Sbosticpattern space.
161*55999SbosticYou can't, however, use a literal newline character in an address or
162*55999Sbosticin the substitute command.
163*55999Sbostic.Sh "Sed Functions"
164*55999SbosticIn the following list of commands, the maximum number of permissible
165*55999Sbosticaddresses for each command is indicated by [0addr], [1addr], or [2addr],
166*55999Sbosticrepresenting zero, one, or two addresses.
167*55999Sbostic.Pp
168*55999SbosticThe argument
169*55999Sbostic.Em text
170*55999Sbosticconsists of one or more lines.
171*55999SbosticTo embed a newline in the text, precede it with a backslash.
172*55999SbosticOther backslashes in text are deleted and the following character
173*55999Sbostictaken literally.
174*55999Sbostic.Pp
175*55999SbosticThe
176*55999Sbostic.Dq r
177*55999Sbosticand
178*55999Sbostic.Dq w
179*55999Sbosticfunctions take an optional file parameter, which should be separated
180*55999Sbosticfrom the function letter by white space.
181*55999SbosticEach file given as an argument to
182*55999Sbostic.Nm sed
183*55999Sbosticis created (or its contents truncated) before any input processing begins.
184*55999Sbostic.Pp
185*55999SbosticThe
186*55999Sbostic.Dq b ,
187*55999Sbostic.Dq r ,
188*55999Sbostic.Dq s ,
189*55999Sbostic.Dq t ,
190*55999Sbostic.Dq w ,
191*55999Sbostic.Dq y ,
192*55999Sbostic.Dq ! ,
193*55999Sbosticand
194*55999Sbostic.Dq \&:
195*55999Sbosticfunctions all accept additional arguments.
196*55999SbosticThe following synopses indicate which arguments have to be separated from
197*55999Sbosticthe function letters by white space characters.
198*55999Sbostic.Pp
199*55999SbosticTwo of the functions take a function-list.
200*55999SbosticThis is a list of
201*55999Sbostic.Nm sed
202*55999Sbosticfunctions separated by newlines, as follows:
203*55999Sbostic.Bd -literal -offset indent
204*55999Sbostic{ function
205*55999Sbostic  function
206*55999Sbostic  ...
207*55999Sbostic  function
208*55999Sbostic}
209*55999Sbostic.Ed
210*55999Sbostic.Pp
211*55999SbosticThe
212*55999Sbostic.Dq {
213*55999Sbosticcan be preceded by white space and can be followed by white space.
214*55999SbosticThe function can be preceded by white space.
215*55999SbosticThe terminating
216*55999Sbostic.Dq }
217*55999Sbosticmust be preceded by a newline an optional white space.
218*55999Sbostic.sp
219*55999Sbostic.Bl -tag -width "XXXXXX" -compact
220*55999Sbostic.It [2addr] function-list
221*55999SbosticExecute function-list only when the pattern space is selected.
222*55999Sbostic.sp
223*55999Sbostic.It [1addr]a\e
224*55999Sbostic.It text
225*55999Sbostic.br
226*55999SbosticWrite
227*55999Sbostic.Em text
228*55999Sbosticto standard output immediately before each attempt to read a line of input,
229*55999Sbosticwhether by executing the
230*55999Sbostic.Dq N
231*55999Sbosticfunction or by beginning a new cycle.
232*55999Sbostic.sp
233*55999Sbostic.It [2addr]b[lable]
234*55999SbosticBranch to the
235*55999Sbostic.Dq \&:
236*55999Sbosticfunction with the specified label.
237*55999SbosticIf the label is not specified, branch to the end of the script.
238*55999Sbostic.sp
239*55999Sbostic.It [2addr]c\e
240*55999Sbostic.It text
241*55999Sbostic.br
242*55999SbosticDelete the pattern space.
243*55999SbosticWith 0 or 1 address or at the end of a 2-address range,
244*55999Sbostic.Em text
245*55999Sbosticis written to the standard output.
246*55999Sbostic.sp
247*55999Sbostic.It [2addr]d
248*55999SbosticDelete the pattern space and start the next cycle.
249*55999Sbostic.sp
250*55999Sbostic.It [2addr]D
251*55999SbosticDelete the initial segment of the pattern space through the first
252*55999Sbosticnewline character and start the next cycle.
253*55999Sbostic.sp
254*55999Sbostic.It [2addr]g
255*55999SbosticReplace the contents of the pattern space with the contents of the
256*55999Sbostichold space.
257*55999Sbostic.sp
258*55999Sbostic.It [2addr]G
259*55999SbosticAppend a newline character followed by the contents of the hold space
260*55999Sbosticto the pattern space.
261*55999Sbostic.sp
262*55999Sbostic.It [2addr]h
263*55999SbosticReplace the contents of the hold space with the contents of the
264*55999Sbosticpattern space.
265*55999Sbostic.sp
266*55999Sbostic.It [2addr]H
267*55999SbosticAppend a newline character followed by the contents of the pattern space
268*55999Sbosticto the hold space.
269*55999Sbostic.sp
270*55999Sbostic.It [1addr]i\e
271*55999Sbostic.It text
272*55999Sbostic.br
273*55999SbosticWrite
274*55999Sbostic.Em text
275*55999Sbosticto the standard output.
276*55999Sbostic.sp
277*55999Sbostic.It [2addr]l
278*55999Sbostic(The letter ell.)
279*55999SbosticWrite the pattern space to the standard output in a visually unambiguous
280*55999Sbosticform.
281*55999SbosticThis form is as follows:
282*55999Sbostic.sp
283*55999Sbostic.Bl -tag -width "carriage-returnXX" -offset indent -compact
284*55999Sbostic.It backslash
285*55999Sbostic\e
286*55999Sbostic.It alert
287*55999Sbostic\ea
288*55999Sbostic.It form-feed
289*55999Sbostic\ef
290*55999Sbostic.It newline
291*55999Sbostic\en
292*55999Sbostic.It carriage-return
293*55999Sbostic\er
294*55999Sbostic.It tab
295*55999Sbostic\et
296*55999Sbostic.It vertical tab
297*55999Sbostic\ev
298*55999Sbostic.El
299*55999Sbostic.Pp
300*55999SbosticNonprintable characters are written as three-digit octal numbers (with a
301*55999Sbosticpreceding backslash) for each byte in the character (most significant byte
302*55999Sbosticfirst).
303*55999SbosticLong lines are folded, with the point of folding indicated by displaying
304*55999Sbostica backslash followed by a newline.
305*55999SbosticThe end of each line is marked with a
306*55999Sbostic.Dq $ .
307*55999Sbostic.sp
308*55999Sbostic.It [2addr]n
309*55999SbosticWrite the pattern space to the standard output if the default output has
310*55999Sbosticnot been suppressed, and replace the pattern space with the next line of
311*55999Sbosticinput.
312*55999Sbostic.sp
313*55999Sbostic.It [2addr]N
314*55999SbosticAppend the next line of input to the pattern space, using an embedded
315*55999Sbosticnewline character to separate the appended material from the original
316*55999Sbosticcontents.
317*55999SbosticNote that the current line number changes.
318*55999Sbostic.sp
319*55999Sbostic.It [2addr]p
320*55999SbosticWrite the pattern space to standard output.
321*55999Sbostic.sp
322*55999Sbostic.It [2addr]P
323*55999SbosticWrite the pattern space, up to the first newline character to the
324*55999Sbosticstandard output.
325*55999Sbostic.sp
326*55999Sbostic.It [1addr]q
327*55999SbosticBranch to the end of the script and quit without starting a new cycle.
328*55999Sbostic.sp
329*55999Sbostic.It [1addr]r file
330*55999SbosticCopy the contents of
331*55999Sbostic.Em file
332*55999Sbosticto the standard output immediately before the next attempt to read a
333*55999Sbosticline of input.
334*55999SbosticIf
335*55999Sbostic.Em file
336*55999Sbosticcannot be read for any reason, it is silently ignored and no error
337*55999Sbosticcondition is set.
338*55999Sbostic.sp
339*55999Sbostic.It [2addr]s/regular expression/replacement/flags
340*55999SbosticSubstitute the replacement string for the first instance of the regular
341*55999Sbosticexpression in the pattern space.
342*55999SbosticAny character other than backslash or newline can be used instead of
343*55999Sbostica slash to delimit the RE and the replacement.
344*55999SbosticWithin the RE and the replacement, the RE delimiter itself can be used as
345*55999Sbostica literal character if it is preceded by a backslash.
346*55999Sbostic.Pp
347*55999SbosticAn ampersand
348*55999Sbostic.Po
349*55999Sbostic.Dq &
350*55999Sbostic.Pc
351*55999Sbosticappearing in the replacement is replaced by the string matching the RE.
352*55999SbosticThe special meaning of
353*55999Sbostic.Dq &
354*55999Sbosticin this context can be suppressed by preceding it by backslash.
355*55999SbosticThe string
356*55999Sbostic.Dq \e# ,
357*55999Sbosticwhere
358*55999Sbostic.Dq #
359*55999Sbosticis a digit, is replaced by the text matched
360*55999Sbosticby the corresponding backreference expression (see
361*55999Sbostic.Xr re_format 7 ).
362*55999Sbostic.Pp
363*55999SbosticA line can be split by substituting a newline character into it.
364*55999SbosticTo specify a newline character in the replacement string, precede it with
365*55999Sbostica backslash.
366*55999Sbostic.Pp
367*55999SbosticThe value of
368*55999Sbostic.Em flags
369*55999Sbosticin the substitute function is zero or more of the following:
370*55999Sbostic.Bl -tag -width "XXXXXX" -offset indent
371*55999Sbostic.It "0 ... 9"
372*55999SbosticMake the substitution only for the N'th occurrence of the regular
373*55999Sbosticexpression in the pattern space.
374*55999Sbostic.It g
375*55999SbosticMake the substitution for all non-overlapping matches of the
376*55999Sbosticregular expression, not just the first one.
377*55999Sbostic.It p
378*55999SbosticWrite the pattern space to standard output if a replacement was made.
379*55999SbosticIf the replacement string is identical to that which it replaces, it
380*55999Sbosticis still considered to have been a replacement.
381*55999Sbostic.It w Em file
382*55999SbosticAppend the pattern space to
383*55999Sbostic.Em file
384*55999Sbosticif a replacement was made.
385*55999SbosticIf the replacement string is identical to that which it replaces, it
386*55999Sbosticis still considered to have been a replacement.
387*55999Sbostic.El
388*55999Sbostic.sp
389*55999Sbostic.It [2addr]t [label]
390*55999SbosticBranch to the
391*55999Sbostic.Dq :
392*55999Sbosticfunction bearing the label if any substitutions have been made since the
393*55999Sbosticmost recent reading of an input line or execution of a
394*55999Sbostic.Dq t
395*55999Sbosticfunction.
396*55999SbosticIf no label is specified, branch to the end of the script.
397*55999Sbostic.sp
398*55999Sbostic.It [2addr]w Em file
399*55999SbosticAppend the pattern space to the
400*55999Sbostic.Em file .
401*55999Sbostic.sp
402*55999Sbostic.It [2addr]x
403*55999SbosticSwap the contents of the pattern and hold spaces.
404*55999Sbostic.sp
405*55999Sbostic.It [2addr]y/string1/string2/
406*55999SbosticReplace all occurrences of characters in
407*55999Sbostic.Em string1
408*55999Sbosticin the pattern space with the corresponding characters from
409*55999Sbostic.Em string2 .
410*55999SbosticAny character other than a backslash or newline can be used instead of
411*55999Sbostica slash to delimit the strings.
412*55999SbosticWithin
413*55999Sbostic.Em string1
414*55999Sbosticand
415*55999Sbostic.Em string2 ,
416*55999Sbosticthe delimiter itself can be used as a literal character if it is preceded
417*55999Sbosticby a backslash.
418*55999Sbostic.sp
419*55999Sbostic.It [2addr]!function
420*55999Sbostic.It [2addr]!function-list
421*55999SbosticApply the function or function-list only to the lines that are
422*55999Sbostic.Em not
423*55999Sbosticselected by the address(es).
424*55999Sbostic.sp
425*55999Sbostic.It [0addr]:label
426*55999SbosticThis function does nothing; it bears a label to which the
427*55999Sbostic.Dq b
428*55999Sbosticand
429*55999Sbostic.Dq t
430*55999Sbosticcommands may branch.
431*55999Sbostic.sp
432*55999Sbostic.It [1addr]=
433*55999SbosticWrite the line number to the standard output followed by a newline
434*55999Sbosticcharacter.
435*55999Sbostic.sp
436*55999Sbostic.It [0addr]
437*55999SbosticEmpty lines are ignored.
438*55999Sbostic.sp
439*55999Sbostic.It [0addr]#
440*55999SbosticThe
441*55999Sbostic.Dq #
442*55999Sbosticand the remainder of the line are ignored (treated as a comment), with
443*55999Sbosticthe single exception that if the first two characters in the file are
444*55999Sbostic.Dq #n ,
445*55999Sbosticthe default output is suppressed.
446*55999SbosticThis is the same as specifying the
447*55999Sbostic.Fl n
448*55999Sbosticoption on the command line.
449*55999Sbostic.El
450*55999Sbostic.Pp
451*55999SbosticThe
452*55999Sbostic.Nm sed
453*55999Sbosticutility exits 0 on success and >0 if an error occurs.
454*55999Sbostic.Sh SEE ALSO
455*55999Sbostic.Xr awk 1 ,
456*55999Sbostic.Xr ed 1 ,
457*55999Sbostic.Xr grep 1 ,
458*55999Sbostic.Xr regex 3 ,
459*55999Sbostic.Xr re_format 7
460*55999Sbostic.Sh HISTORY
461*55999SbosticA
462*55999Sbostic.Nm sed
463*55999Sbosticcommand appeared in
464*55999Sbostic.At v7 .
465*55999Sbostic.Sh STANDARDS
466*55999SbosticThe
467*55999Sbostic.Nm sed
468*55999Sbosticfunction is expected to be a superset of the
469*55999Sbostic.St -p1003.2
470*55999Sbosticspecification.
471