xref: /csrg-svn/old/sed/sed.1 (revision 55688)
148718Scael.\" Copyright (c) 1991 The Regents of the University of California.
248309Sbostic.\" All rights reserved.
319460Smckusick.\"
450205Sbostic.\" This code is derived from software contributed to Berkeley by
550205Sbostic.\" the Institute of Electrical and Electronics Engineers, Inc.
650205Sbostic.\"
748718Scael.\" %sccs.include.redist.roff%
843734Scael.\"
9*55688Sbostic.\"     @(#)sed.1	6.7 (Berkeley) 07/25/92
1048309Sbostic.\"
1148718Scael.Dd
1248718Scael.Dt SED 1
1348718Scael.Os
1448718Scael.Sh NAME
1548718Scael.Nm sed
1648718Scael.Nd stream editor.
1748718Scael.Sh SYNOPSIS
1848718Scael.Nm sed
1948718Scael.Op Fl n
2048718Scael.Ar script
2148718Scael.Ar
2248718Scael.Nm sed
2348718Scael.Op Fl n
2448718Scael.Op Fl e Ar script
2548718Scael.Op Fl f Ar script_file
2648718Scael.Ar
2748718Scael.Sh DESCRIPTION
2819460SmckusickThe
2948718Scael.Nm sed
3048718Scaelutility is a stream editor which reads one or more
3148718Scaeltext files, applies given editing command scripts,
3248718Scaeland writes the results to standard output.
3348718ScaelThe script of editing commands can be given in the command
3448718Scaelline, or can be contained in the file
3548718Scael.Ar script_file .
3648718Scael.Pp
3748718ScaelOptions:
3848718Scael.Bl -tag -width indent
3948718Scael.It Fl e Ar script
4048718ScaelThe command line
4148718Scael.Ar script
4248718Scaelis used to edit the input.
4348718ScaelIf multiple
4448718Scael.Fl e
4548718Scaeloptions are given, the scripts are
4648718Scaelapplied in the order given to each line of the
4748718Scaelinput files.
4848718ScaelIf a
4948718Scael.Fl f
5048718Scaeloption is given in addition
5148718Scaelto
5248718Scael.Fl e ,
5348718Scaellines are acted upon by scripts first.
5448718Scael.It Fl f Ar script_file
5548718ScaelThe file
5648718Scael.Ar script_file
5748718Scaelis expected to contain editing commands, one per line,
5848718Scaeland these commands are applied to the input.
5948718ScaelIf multiple
6048718Scael.Fl f
6148718Scaeloptions are given, the commands in the
6248718Scael.Ar script_file Ns s
6348718Scaelare applied in
6448718Scaelthe order given to each line of the input
6548718Scaelfiles.
6648718ScaelIf a
6748718Scael.Fl e
6848718Scaeloption is given in addition to
6948718Scael.Fl f ,
7048718Scaellines are acted upon by
7148718Scaelthe commands in the
7248718Scael.At script_file Ns s
7348718Scaelfirst.
7448718Scael.It Fl n
7519460SmckusickThe
7648718Scael.Fl n
7748718Scaeloption suppresses the default output, which normally
7848718Scaelpasses each line, after it is examined for editing,
7948718Scaelto standard output.
8048718ScaelTherefore, only lines explicitly
8148718Scaelselected for output are written.
8248718Scael.El
8348718Scael.Pp
8448718ScaelThe following operands are available:
8548718Scael.Bl -tag -width file
8648718Scael.It Ar file
8748718ScaelA pathname of a file whose contents are read and
8848718Scaeledited.
8948718ScaelIf multiple file operands are given,
9048718Scaelthe named files are read in the order given and
9148718Scaelthe concatenation is edited.
9248718ScaelIf no file operands
9348718Scaelare given, the standard input is used.
9448718Scael.It Ar script
9548718ScaelThe script consists of one or more editing
9648718Scaelinstructions that are entered on the command line.
9748718Scael.El
9848718Scael.\" .Pp
9948718Scael.\" The following environment variable affects the execution of
10048718Scael.\" sed:
10148718Scael.\" .It Ev LC_CTYPE
10248718Scael.\" The locale for character classification.
10348718Scael.Pp
10448718Scael.Bd -filled -offset indent -compact
10548718Scael.Op address Op ,address
10648718Scaelfunction
10748718Scael.Op arguments
10848718Scael.Ed
10948718Scael.Pp
11048718ScaelIn default operation,
11148718Scael.Nm sed
11248718Scaelcyclically copies a line of input
11348718Scaelinto a pattern space (unless there is something left after a
11448718Scael.Cm D
11548718Scaelcommand), applies in sequence all commands whose addresses
11648718Scaelselect that pattern space, and at the end of the script
11748718Scaelcopies the pattern space to the standard output (except
11848718Scaelunder
11948718Scael.Fl n )
12019460Smckusickand deletes the pattern space.
12148718Scael.Pp
12248718ScaelSome of the commands use a hold space to save all or part of
12348718Scaelthe pattern space for
12448718Scaelsubsequent retrieval.
12548718Scael.\" The pattern and hold spaces are each
12648718Scael.\" limited to
12748718Scael.\" .Pf { Dv SED_PATTERN_MAX Ns }
12848718Scael.\" bytes.
12948718Scael.Pp
13048718ScaelAn address is either no address; a decimal number that
13148718Scaelcounts input lines cumulatively across files; a
13248718Scael.Ql $
13348718Scaelthat
13448718Scaeladdresses the last line of input; a context address; or
13548718Scaelregular expression.
13648718Scael.Pp
13748718ScaelA command line with no addresses selects every pattern
13848718Scaelspace.
13948718Scael.Pp
14048718ScaelA command line with one address selects each pattern space
14148718Scaelthat matches the address.
14248718Scael.Pp
14348718ScaelA command line with two addresses selects the inclusive
14448718Scaelrange from the first pattern space that matches the first
14548718Scaeladdress through the next pattern space which matches the
14648718Scaelsecond.
14748718Scael(If the second address is a number less than or
14848718Scaelequal to the line number first selected, only one line is
14948718Scaelselected.)
15048718ScaelStarting at the first line following the
15148718Scaelselected range,
15248718Scael.Nm sed
15348718Scaellooks again for the first address.
15448718ScaelThereafter the process is repeated.
15548718Scael.Pp
15619460SmckusickEditing commands can be applied only to non-selected pattern
15748718Scaelspaces by use of the negation function
15848718Scael.Cm \&!
15948718Scael(See below.)
16048718Scael.Pp
16148718ScaelThe
16248718Scael.Nm sed
16348718Scaelutility uses basic regular expressions, as are found in the
16448718Scaeleditor
16548718Scael.Xr ed 1 ,
16648718Scaelwith the following additions:
16748718Scael.Pp
16848718Scael.Bl -enum -offset indent
16948718Scael.It
170*55688SbosticIn a context address, the construction
171*55688Sbostic.Li \e?RE? ,
172*55688Sbosticwhere ?
173*55688Sbosticis any character, is identical to
174*55688Sbostic.Li /RE/ .
175*55688SbosticNote that in the
176*55688Sbosticcontext address
177*55688Sbostic.Li \exabc\exdefx ,
178*55688Sbosticthe second
179*55688Sbostic.Ql x
180*55688Sbosticstands for
181*55688Sbosticitself, so that the regular expression is
182*55688Sbostic.Li abcxdef .
18348718Scael.It
184*55688SbosticThe escape sequence
185*55688Sbostic.Ql \en
186*55688Sbosticmatches a <newline> embedded
18748718Scaelin the pattern space.
18848718Scael.It
189*55688SbosticA period
190*55688Sbostic.Ql \&.
191*55688Sbosticmatches any character except the terminal <newline> of the pattern space.
19248718Scael.El
19348718Scael.Pp
19448718ScaelIn the following list of functions the maximum number of
19548718Scaelpermissible addresses for each function is indicated by
19648718Scael.Op 0addr ,
19748718Scael.Op 1addr ,
19819460Smckusickor
19948718Scael.Op 2addr ;
20048718Scaelrepresenting zero, one, or two
20148718Scaeladdresses.
20248718Scael.Pp
20348718ScaelThe argument text consists of one or more lines.
20419460SmckusickEach
20548718Scaelembedded <newline> in the text shall be preceded by a
20648718Scaelbackslash.
20748718ScaelOther backslashes in text are treated like
20848718Scaelbackslashes in the replacement string of an s command, and
20948718Scaelcan be used to protect initial <blank>s against the stripping
21048718Scaelthat is done on every script line.
21148718Scael.Pp
21248718ScaelThe
21348718Scael.Cm r
21448718Scaeland
21548718Scael.Cm w
21648718Scaelcommands take an optional
21748718Scael.Ar rfile
21848718Scael(or
21948718Scael.Ar wfile )
22048718Scaelparameter, separated from the command letter by zero or more
22148718Scael<blank>s.
22248718Scael.Pp
22348718ScaelThe argument
22448718Scael.Ar rfile
22548718Scaelor the argument
22648718Scael.Ar wfile
22748718Scaelshall terminate the
22848718Scaelcommand line.
22948718ScaelEach
23048718Scael.Ar wfile
23148718Scaelis created before processing
23248718Scaelbegins.
23348718ScaelThere can be at most ten distinct
23448718Scael.Ar wfile
23548718Scaelarguments
23648718Scaelin the script.
23748718Scael.Pp
23848718ScaelThe
23948718Scael.Cm b , r , s , t ,
24048718Scael.Cm w , y , \&! ,
24148718Scaeland
24248718Scael.Cm \&:
24348718Scaelcommands take additional
24419460Smckusickarguments.
24548718ScaelThe following synopses indicate which arguments
24648718Scaelare separated from the commands by <blank>s.
24748718Scael.Bl -tag -width addrcommandxx
248*55688Sbostic.It Xo
249*55688Sbostic.Oo Ad 2addr Oc \&{ command_list \&}
250*55688Sbostic.Xc
25148718ScaelExecutes command_list only when the pattern
25248718Scaelspace is selected.
25348718ScaelThe {} braces can be preceded
25448718Scaeland followed by white space.
255*55688Sbostic.It Xo
256*55688Sbostic.Oo Ad 1addr Oc Ns Cm a Ar text
257*55688Sbostic.Xc
25848718ScaelWrites text to the standard output after the
25948718Scaelpattern space is written.
260*55688Sbostic.It Xo
261*55688Sbostic.Oo Ad 2addr Oc Ns Cm b Ar label
262*55688Sbostic.Xc
26348718ScaelBranches to the
26448718Scael.Cm \&:
26548718Scaelcommand bearing the label.
26648718ScaelIf label is empty, branch to the end of the
26748718Scaelscript.
268*55688Sbostic.It Xo
269*55688Sbostic.Oo Ad 2addr Oc Ns Cm c Ar text
270*55688Sbostic.Xc
27148718ScaelDeletes the pattern space.
27248718ScaelWith 0 or 1
27348718Scaeladdress or at the end of a 2-address range,
27448718Scaelplaces text on the output.
275*55688Sbostic.It Xo
276*55688Sbostic.Oo Ad 2addr Oc Ns Cm d
277*55688Sbostic.Xc
27848718ScaelDeletes the pattern space and starts the next
27948718Scaelcycle.
280*55688Sbostic.It Xo
281*55688Sbostic.Oo Ad 2addr Oc Ns Cm D
282*55688Sbostic.Xc
28348718ScaelDeletes the initial segment of the pattern
28448718Scaelspace through the first <newline> and starts
28548718Scaelthe next cycle.
286*55688Sbostic.It Xo
287*55688Sbostic.Oo Ad 2addr Oc Ns Cm g
288*55688Sbostic.Xc
28948718ScaelReplaces the contents of the pattern space by
29048718Scaelthe contents of the hold space.
291*55688Sbostic.It Xo
292*55688Sbostic.Oo Ad 2addr Oc Ns Cm G
293*55688Sbostic.Xc
29448718ScaelAppends the contents of the hold space to the
29548718Scaelpattern space.
296*55688Sbostic.It Xo
297*55688Sbostic.Oo Ad 2addr Oc Ns Cm h
298*55688Sbostic.Xc
29948718ScaelReplaces the contents of the hold space by
30048718Scaelthe contents of the pattern space.
301*55688Sbostic.It Xo
302*55688Sbostic.Oo Ad 2addr Oc Ns Cm H
303*55688Sbostic.Xc
30448718ScaelAppends the contents of the pattern space to
30548718Scaelthe hold space.
306*55688Sbostic.It Xo
307*55688Sbostic.Oo Ad 1addr Oc Ns Cm i Ar text
308*55688Sbostic.Xc
30948718ScaelWrites text to the standard output before the
31048718Scaelpattern space is written.
311*55688Sbostic.It Xo
312*55688Sbostic.Oo Ad 2addr Oc Ns Cm l
313*55688Sbostic.Xc
31448718ScaelLists the pattern space on the standard out-
31548718Scaelput in an unambiguous form.
31648718ScaelNonprinting
31748718Scaelcharacters are listed as hexadecimal digit
31848718Scaelpairs, with a preceding backslash, with the
31948718Scaelfollowing exceptions:
320*55688Sbostic.Bl -column <carriagexreturn> -offset indent
32148718Scael<alert>	\ea
32248718Scael<backslash>	\e\e
32348718Scael<backspace>	\eb
32448718Scael<carriage return>	\er
32548718Scael<form-feed>	\ef
32648718Scael<newline>	\en
32748718Scael<tab>	\et
32848718Scael<vertical tab>	\ev
32948718Scael.El
33048718Scael.Pp
33148718ScaelLong lines are folded; the length at which
33248718Scaelfolding occurs is ungiven, but should be
33348718Scaelappropriate for the output device.
334*55688Sbostic.It Xo
335*55688Sbostic.Oo Ad 2addr Oc Ns Cm n
336*55688Sbostic.Xc
33748718ScaelCopies the pattern space to the standard output
33848718Scaeland replaces the pattern space with the
33948718Scaelnext line of input.
340*55688Sbostic.It Xo
341*55688Sbostic.Oo Ad 2addr Oc Ns Cm N
342*55688Sbostic.Xc
34348718ScaelAppends the next line of input to the pattern
34448718Scaelspace, using an embedded <newline> to
34548718Scaelseparate the appended material from the
34648718Scaeloriginal material.
34748718ScaelNote that the current line
34848718Scaelnumber changes.
349*55688Sbostic.It Xo
350*55688Sbostic.Oo Ad 2addr Oc Ns Cm p
351*55688Sbostic.Xc
35248718ScaelCopies
35348718Scael.Op prints
35448718Scaelthe pattern space to the
35548718Scaelstandard output.
356*55688Sbostic.It Xo
357*55688Sbostic.Oo Ad 2addr Oc Ns Cm P
358*55688Sbostic.Xc
35948718ScaelCopies
36048718Scael.Op prints
36148718Scaelthe pattern space, up to the
36248718Scaelfirst <newline>, to the standard output.
363*55688Sbostic.It Xo
364*55688Sbostic.Oo Ad 1addr Oc Ns Cm q
365*55688Sbostic.Xc
36648718ScaelBranches to the end of the script and quits
36748718Scaelwithout starting a new cycle.
368*55688Sbostic.It Xo
369*55688Sbostic.Oo Ad 1addr Oc Ns Cm r Ar rfile
370*55688Sbostic.Xc
37148718ScaelRead the contents of rfile.
37248718ScaelPlace them on the
37348718Scaeloutput before reading the next input line.
374*55688Sbostic.It Xo
375*55688Sbostic.Oo Ad 2addr Oc Ns \\*(cMs\\*(dF/\\*(aRregular expression\\*(dF/\\*(aRreplacement string\\*(dF/flags
376*55688Sbostic.Xc
37748718ScaelSubstitutes the replacement string for
37848718Scaelinstances of the regular expression in the
37948718Scaelpattern space.
38048718ScaelAny character can be used
38148718Scaelinstead of
38248718Scael.Ql / .
38348718ScaelThe value of flags is zero or
38448718Scaelmore of:
38548718Scael.Bl -tag -width Ds
38648718Scael.It Ar n
387*55688Sbosticn=1-512.  Substitutes for the
388*55688Sbostic.Ar n Ns th
38948718Scaeloccurrence only of the regular
39048718Scaelexpression found within the pattern space.
39148718Scael.It Cm g
39248718ScaelGlobally substitutes for all
39348718Scaelnon-overlapping instances of the regular
39448718Scaelexpression rather than just
39548718Scaelthe first one.  If both
39648718Scael.Cm g
39748718Scaeland
39848718Scael.Cm n
39948718Scaelare given,
40048718Scael.Cm g
40148718Scaeltakes precedence.
40248718Scael.It Cm p
40348718ScaelPrints the pattern space if a
40448718Scaelreplacement was made.
40548718Scael.It Cm w Ar wfile
40648718ScaelWrite.  Appends the pattern space
40748718Scaelto
40848718Scael.Ar wfile
40948718Scaelif a replacement was
41048718Scaelmade.
41148718Scael.El
412*55688Sbostic.It Xo
413*55688Sbostic.Oo Ad 2addr Oc Ns Cm t Ar label
414*55688Sbostic.Xc
41519460SmckusickTest.
41648718ScaelBranches to the
41748718Scael.Cm \&:
41848718Scaelcommand bearing the
41948718Scaellabel if any substitutions have been made
42048718Scaelsince the most recent reading of an input
42148718Scaelline or execution of a
42248718Scael.Cm t .
42348718ScaelIf label is empty,
42448718Scaelbranches to the end of the script.
425*55688Sbostic.It Xo
426*55688Sbostic.Oo Ad 2addr Oc Ns Cm w Ar wfile
427*55688Sbostic.Xc
42848718ScaelAppends
42948718Scael.Op writes
43048718Scaelthe pattern space to
43148718Scael.Ar wfile .
432*55688Sbostic.It Xo
433*55688Sbostic.Oo Ad 2addr Oc Ns Cm x
434*55688Sbostic.Xc
43548718ScaelExchanges the contents of the pattern and
43648718Scaelhold spaces.
437*55688Sbostic.It Xo
438*55688Sbostic.Oo Ad 2addr Oc Ns \\*(cMy\\*(dF/\\*(aRstring1\\*(dF/\\*(aRstring2\\*(dF/
439*55688Sbostic.Xc
44048718ScaelReplaces all occurrences of collating
44148718Scaelelements in
44248718Scael.Ar string1
44348718Scaelwith the corresponding
44448718Scaelcollating element in
44548718Scael.Ar string2 .
44619460SmckusickThe lengths of
44748718Scael.Ar string1
44819460Smckusickand
44948718Scael.Ar string2
45048718Scaelshall be equal.
451*55688Sbostic.It Xo
452*55688Sbostic.Oo Ad 2addr Oc Ns \\*(cM!\\*(dFfunction
453*55688Sbostic.Xc
45448718ScaelApplies the function (or group, if function
45548718Scaelis {) only to the lines that are not selected
45648718Scaelby the address(es).
457*55688Sbostic.It Xo
458*55688Sbostic.Oo Ad 0addr Oc Ns \\*(cM:\\*(dFlabel
459*55688Sbostic.Xc
46048718ScaelThis command does nothing; it bears a label
46148718Scaelfor the b and t commands to branch to.
462*55688Sbostic.It Xo
463*55688Sbostic.Oo Ad 1addr Oc Ns Cm \&=
464*55688Sbostic.Xc
46548718ScaelPlaces the current line number on the standard
46648718Scaeloutput as a line with its own line
46748718Scaelnumber.
468*55688Sbostic.It Xo
469*55688Sbostic.Oo Ad 0addr Oc
470*55688Sbostic.Xc
47119460SmckusickAn empty command is ignored.
472*55688Sbostic.It Xo
473*55688Sbostic.Oo Ad 0addr Oc Cm #
474*55688Sbostic.Xc
47548718ScaelIf a
47648718Scael.Cm #
47748718Scaelappears as the first character on any
47848718Scaelline of a script file, that entire line is
47948718Scaelignored (treated as a comment), with the single
48048718Scaelexception that if the first line of the
48148718Scaelscript file begins with
48248718Scael.Cm Ns #n ,
48348718Scaelthe default
48448718Scaeloutput is suppressed.
48548718Scael.El
48648718Scael.Pp
48748718ScaelThe
48848718Scael.Nm sed
48948718Scaelutility exits 0 on success, and >0 if an error occurs.
49048718Scael.Pp
49148718ScaelIf one or more of the input (not script) files cannot be
49248718Scaelopened for reading,
49348718Scael.Nm sed
49448718Scaelcontinues to process the remaining
49548718Scaelfiles.
49648718Scael.Sh STANDARDS
49748718ScaelThe
49848718Scael.Nm sed
49948718Scaelutility is expected to be
50048718Scael.St -p1003.2
50148718Scaelcompatible.
502