xref: /csrg-svn/old/sed/sed.1 (revision 50205)
148718Scael.\" Copyright (c) 1991 The Regents of the University of California.
248309Sbostic.\" All rights reserved.
319460Smckusick.\"
4*50205Sbostic.\" This code is derived from software contributed to Berkeley by
5*50205Sbostic.\" the Institute of Electrical and Electronics Engineers, Inc.
6*50205Sbostic.\"
748718Scael.\" %sccs.include.redist.roff%
843734Scael.\"
9*50205Sbostic.\"     @(#)sed.1	6.6 (Berkeley) 06/27/91
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
17048718ScaelIn a context address, the construction \e?RE?, where ?
17148718Scaelis any character, is identical to /RE/.  Note that in the
17248718Scaelcontext address \exabc\exdefx, the second x stands for
17348718Scaelitself, so that the regular expression is abcxdef.
17448718Scael.It
17548718ScaelThe escape sequence \en matches a <newline> embedded
17648718Scaelin the pattern space.
17748718Scael.It
17848718ScaelA period (.) matches any character except the termi-
17948718Scaelnal <newline> of the pattern space.
18048718Scael.El
18148718Scael.Pp
18248718ScaelIn the following list of functions the maximum number of
18348718Scaelpermissible addresses for each function is indicated by
18448718Scael.Op 0addr ,
18548718Scael.Op 1addr ,
18619460Smckusickor
18748718Scael.Op 2addr ;
18848718Scaelrepresenting zero, one, or two
18948718Scaeladdresses.
19048718Scael.Pp
19148718ScaelThe argument text consists of one or more lines.
19219460SmckusickEach
19348718Scaelembedded <newline> in the text shall be preceded by a
19448718Scaelbackslash.
19548718ScaelOther backslashes in text are treated like
19648718Scaelbackslashes in the replacement string of an s command, and
19748718Scaelcan be used to protect initial <blank>s against the stripping
19848718Scaelthat is done on every script line.
19948718Scael.Pp
20048718ScaelThe
20148718Scael.Cm r
20248718Scaeland
20348718Scael.Cm w
20448718Scaelcommands take an optional
20548718Scael.Ar rfile
20648718Scael(or
20748718Scael.Ar wfile )
20848718Scaelparameter, separated from the command letter by zero or more
20948718Scael<blank>s.
21048718Scael.Pp
21148718ScaelThe argument
21248718Scael.Ar rfile
21348718Scaelor the argument
21448718Scael.Ar wfile
21548718Scaelshall terminate the
21648718Scaelcommand line.
21748718ScaelEach
21848718Scael.Ar wfile
21948718Scaelis created before processing
22048718Scaelbegins.
22148718ScaelThere can be at most ten distinct
22248718Scael.Ar wfile
22348718Scaelarguments
22448718Scaelin the script.
22548718Scael.Pp
22648718ScaelThe
22748718Scael.Cm b , r , s , t ,
22848718Scael.Cm w , y , \&! ,
22948718Scaeland
23048718Scael.Cm \&:
23148718Scaelcommands take additional
23219460Smckusickarguments.
23348718ScaelThe following synopses indicate which arguments
23448718Scaelare separated from the commands by <blank>s.
23548718Scael.Bl -tag -width addrcommandxx
23648718Scael.It Oo Ad 2addr Oc \&{ command_list \&}
23748718ScaelExecutes command_list only when the pattern
23848718Scaelspace is selected.
23948718ScaelThe {} braces can be preceded
24048718Scaeland followed by white space.
24148718Scael.It Oo Ad 1addr Oc Ns Cm a Ar text
24248718ScaelWrites text to the standard output after the
24348718Scaelpattern space is written.
24448718Scael.It Oo Ad 2addr Oc Ns Cm b Ar label
24548718ScaelBranches to the
24648718Scael.Cm \&:
24748718Scaelcommand bearing the label.
24848718ScaelIf label is empty, branch to the end of the
24948718Scaelscript.
25048718Scael.It Oo Ad 2addr Oc Ns Cm c Ar text
25148718ScaelDeletes the pattern space.
25248718ScaelWith 0 or 1
25348718Scaeladdress or at the end of a 2-address range,
25448718Scaelplaces text on the output.
25548718Scael.It Oo Ad 2addr Oc Ns Cm d
25648718ScaelDeletes the pattern space and starts the next
25748718Scaelcycle.
25848718Scael.It Oo Ad 2addr Oc Ns Cm D
25948718ScaelDeletes the initial segment of the pattern
26048718Scaelspace through the first <newline> and starts
26148718Scaelthe next cycle.
26248718Scael.It Oo Ad 2addr Oc Ns Cm g
26348718ScaelReplaces the contents of the pattern space by
26448718Scaelthe contents of the hold space.
26548718Scael.It Oo Ad 2addr Oc Ns Cm G
26648718ScaelAppends the contents of the hold space to the
26748718Scaelpattern space.
26848718Scael.It Oo Ad 2addr Oc Ns Cm h
26948718ScaelReplaces the contents of the hold space by
27048718Scaelthe contents of the pattern space.
27148718Scael.It Oo Ad 2addr Oc Ns Cm H
27248718ScaelAppends the contents of the pattern space to
27348718Scaelthe hold space.
27448718Scael.It Oo Ad 1addr Oc Ns Cm i Ar text
27548718ScaelWrites text to the standard output before the
27648718Scaelpattern space is written.
27748718Scael.It Oo Ad 2addr Oc Ns Cm l
27848718ScaelLists the pattern space on the standard out-
27948718Scaelput in an unambiguous form.
28048718ScaelNonprinting
28148718Scaelcharacters are listed as hexadecimal digit
28248718Scaelpairs, with a preceding backslash, with the
28348718Scaelfollowing exceptions:
28448718Scael.Pp
28548718Scael.ne 8
28648718Scael.Bd -filled -offset indent -compact
28748718Scael.Bl -column <carriagexreturn>
28848718Scael<alert>	\ea
28948718Scael<backslash>	\e\e
29048718Scael<backspace>	\eb
29148718Scael<carriage return>	\er
29248718Scael<form-feed>	\ef
29348718Scael<newline>	\en
29448718Scael<tab>	\et
29548718Scael<vertical tab>	\ev
29648718Scael.El
29748718Scael.Ed
29848718Scael.Pp
29948718ScaelLong lines are folded; the length at which
30048718Scaelfolding occurs is ungiven, but should be
30148718Scaelappropriate for the output device.
30248718Scael.It Oo Ad 2addr Oc Ns Cm n
30348718ScaelCopies the pattern space to the standard output
30448718Scaeland replaces the pattern space with the
30548718Scaelnext line of input.
30648718Scael.It Oo Ad 2addr Oc Ns Cm N
30748718ScaelAppends the next line of input to the pattern
30848718Scaelspace, using an embedded <newline> to
30948718Scaelseparate the appended material from the
31048718Scaeloriginal material.
31148718ScaelNote that the current line
31248718Scaelnumber changes.
31348718Scael.It Oo Ad 2addr Oc Ns Cm p
31448718ScaelCopies
31548718Scael.Op prints
31648718Scaelthe pattern space to the
31748718Scaelstandard output.
31848718Scael.It Oo Ad 2addr Oc Ns Cm P
31948718ScaelCopies
32048718Scael.Op prints
32148718Scaelthe pattern space, up to the
32248718Scaelfirst <newline>, to the standard output.
32348718Scael.It Oo Ad 1addr Oc Ns Cm q
32448718ScaelBranches to the end of the script and quits
32548718Scaelwithout starting a new cycle.
32648718Scael.It Oo Ad 1addr Oc Ns Cm r Ar rfile
32748718ScaelRead the contents of rfile.
32848718ScaelPlace them on the
32948718Scaeloutput before reading the next input line.
33048718Scael.It Oo Ad 2addr Oc Ns \\*(cMs\\*(dF/\\*(aRregular expression\\*(dF/\\*(aRreplacement string\\*(dF/flags
33148718ScaelSubstitutes the replacement string for
33248718Scaelinstances of the regular expression in the
33348718Scaelpattern space.
33448718ScaelAny character can be used
33548718Scaelinstead of
33648718Scael.Ql / .
33748718ScaelThe value of flags is zero or
33848718Scaelmore of:
33948718Scael.Bl -tag -width Ds
34048718Scael.It Ar n
34148718Scaeln=1-512.  Substitutes for the nth
34248718Scaeloccurrence only of the regular
34348718Scaelexpression found within the pattern space.
34448718Scael.It Cm g
34548718ScaelGlobally substitutes for all
34648718Scaelnon-overlapping instances of the regular
34748718Scaelexpression rather than just
34848718Scaelthe first one.  If both
34948718Scael.Cm g
35048718Scaeland
35148718Scael.Cm n
35248718Scaelare given,
35348718Scael.Cm g
35448718Scaeltakes precedence.
35548718Scael.It Cm p
35648718ScaelPrints the pattern space if a
35748718Scaelreplacement was made.
35848718Scael.It Cm w Ar wfile
35948718ScaelWrite.  Appends the pattern space
36048718Scaelto
36148718Scael.Ar wfile
36248718Scaelif a replacement was
36348718Scaelmade.
36448718Scael.El
36548718Scael.It Oo Ad 2addr Oc Ns Cm t Ar label
36619460SmckusickTest.
36748718ScaelBranches to the
36848718Scael.Cm \&:
36948718Scaelcommand bearing the
37048718Scaellabel if any substitutions have been made
37148718Scaelsince the most recent reading of an input
37248718Scaelline or execution of a
37348718Scael.Cm t .
37448718ScaelIf label is empty,
37548718Scaelbranches to the end of the script.
37648718Scael.It Oo Ad 2addr Oc Ns Cm w Ar wfile
37748718ScaelAppends
37848718Scael.Op writes
37948718Scaelthe pattern space to
38048718Scael.Ar wfile .
38148718Scael.It Oo Ad 2addr Oc Ns Cm x
38248718ScaelExchanges the contents of the pattern and
38348718Scaelhold spaces.
38448718Scael.It Oo Ad 2addr Oc Ns \\*(cMy\\*(dF/\\*(aRstring1\\*(dF/\\*(aRstring2\\*(dF/
38548718ScaelReplaces all occurrences of collating
38648718Scaelelements in
38748718Scael.Ar string1
38848718Scaelwith the corresponding
38948718Scaelcollating element in
39048718Scael.Ar string2 .
39119460SmckusickThe lengths of
39248718Scael.Ar string1
39319460Smckusickand
39448718Scael.Ar string2
39548718Scaelshall be equal.
39648718Scael.It Oo Ad 2addr Oc Ns \\*(cM!\\*(dFfunction
39748718ScaelApplies the function (or group, if function
39848718Scaelis {) only to the lines that are not selected
39948718Scaelby the address(es).
40048718Scael.It Oo Ad 0addr Oc Ns \\*(cM:\\*(dFlabel
40148718ScaelThis command does nothing; it bears a label
40248718Scaelfor the b and t commands to branch to.
40348718Scael.It Oo Ad 1addr Oc Ns Cm \&=
40448718ScaelPlaces the current line number on the standard
40548718Scaeloutput as a line with its own line
40648718Scaelnumber.
40748718Scael.It Oo Ad 0addr Oc
40819460SmckusickAn empty command is ignored.
40948718Scael.It Oo Ad 0addr Oc Cm #
41048718ScaelIf a
41148718Scael.Cm #
41248718Scaelappears as the first character on any
41348718Scaelline of a script file, that entire line is
41448718Scaelignored (treated as a comment), with the single
41548718Scaelexception that if the first line of the
41648718Scaelscript file begins with
41748718Scael.Cm Ns #n ,
41848718Scaelthe default
41948718Scaeloutput is suppressed.
42048718Scael.El
42148718Scael.Pp
42248718ScaelThe
42348718Scael.Nm sed
42448718Scaelutility exits 0 on success, and >0 if an error occurs.
42548718Scael.Pp
42648718ScaelIf one or more of the input (not script) files cannot be
42748718Scaelopened for reading,
42848718Scael.Nm sed
42948718Scaelcontinues to process the remaining
43048718Scaelfiles.
43148718Scael.Sh STANDARDS
43248718ScaelThe
43348718Scael.Nm sed
43448718Scaelutility is expected to be
43548718Scael.St -p1003.2
43648718Scaelcompatible.
437