xref: /csrg-svn/usr.bin/sed/sed.1 (revision 65227)
162231Sbostic.\" Copyright (c) 1992, 1993
262231Sbostic.\"	The Regents of the University of California.  All rights reserved.
355999Sbostic.\"
455999Sbostic.\" This code is derived from software contributed to Berkeley by
555999Sbostic.\" the Institute of Electrical and Electronics Engineers, Inc.
655999Sbostic.\"
755999Sbostic.\" %sccs.include.redist.roff%
855999Sbostic.\"
9*65227Smckusick.\"	@(#)sed.1	8.2 (Berkeley) 12/30/93
1055999Sbostic.\"
1155999Sbostic.Dd ""
1255999Sbostic.Dt SED 1
1355999Sbostic.Os
1455999Sbostic.Sh NAME
1555999Sbostic.Nm sed
1655999Sbostic.Nd stream editor
1755999Sbostic.Sh SYNOPSIS
1855999Sbostic.Nm sed
1955999Sbostic.Op Fl an
2055999Sbostic.Ar command
2155999Sbostic.Op Ar file ...
2255999Sbostic.Nm sed
2355999Sbostic.Op Fl an
2455999Sbostic.Op Fl e Ar command
2555999Sbostic.Op Fl f Ar command_file
2655999Sbostic.Op Ar file ...
2755999Sbostic.Sh DESCRIPTION
2855999SbosticThe
2955999Sbostic.Nm sed
3055999Sbosticutility reads the specified files, or the standard input if no files
3155999Sbosticare specified, modifying the input as specified by a list of commands.
3255999SbosticThe input is then written to the standard output.
3355999Sbostic.Pp
3455999SbosticA single command may be specified as the first argument to
3555999Sbostic.Nm sed .
3655999SbosticMultiple commands may be specified by using the
3755999Sbostic.Fl e
3855999Sbosticor
3955999Sbostic.Fl f
4055999Sbosticoptions.
4155999SbosticAll commands are applied to the input in the order they are specified
4255999Sbosticregardless of their origin.
4355999Sbostic.Pp
4455999SbosticThe following options are available:
4555999Sbostic.Bl -tag -width indent
4655999Sbostic.It Fl a
4755999SbosticThe files listed as parameters for the
4855999Sbostic.Dq w
4955999Sbosticfunctions are created (or truncated) before any processing begins,
5055999Sbosticby default.
5155999SbosticThe
5255999Sbostic.Fl a
5355999Sbosticoption causes
5455999Sbostic.Nm sed
5555999Sbosticto delay opening each file until a command containing the related
5655999Sbostic.Dq w
5755999Sbosticfunction is applied to a line of input.
5855999Sbostic.It Fl e Ar command
5955999SbosticAppend the editing commands specified by the
6055999Sbostic.Ar command
6155999Sbosticargument
6255999Sbosticto the list of commands.
6355999Sbostic.It Fl f Ar command_file
6455999SbosticAppend the editing commands found in the file
6555999Sbostic.Ar command_file
6655999Sbosticto the list of commands.
6755999SbosticThe editing commands should each be listed on a separate line.
6855999Sbostic.It Fl n
6955999SbosticBy default, each line of input is echoed to the standard output after
7055999Sbosticall of the commands have been applied to it.
7155999SbosticThe
7255999Sbostic.Fl n
7355999Sbosticoption suppresses this behavior.
7455999Sbostic.El
7555999Sbostic.Pp
7655999SbosticThe form of a
7755999Sbostic.Nm sed
7855999Sbosticcommand is as follows:
7955999Sbostic.sp
8055999Sbostic.Dl [address[,address]]function[arguments]
8155999Sbostic.sp
8255999SbosticWhitespace may be inserted before the first address and the function
8355999Sbosticportions of the command.
8455999Sbostic.Pp
8555999SbosticNormally,
8655999Sbostic.Nm sed
8755999Sbosticcyclically copies a line of input, not including its terminating newline
8855999Sbosticcharacter, into a
8955999Sbostic.Em "pattern space" ,
9055999Sbostic(unless there is something left after a
9155999Sbostic.Dq D
9255999Sbosticfunction),
9355999Sbosticapplies all of the commands with addresses that select that pattern space,
9455999Sbosticcopies the pattern space to the standard output, appending a newline, and
9555999Sbosticdeletes the pattern space.
9655999Sbostic.Pp
9755999SbosticSome of the functions use a
9855999Sbostic.Em "hold space"
9955999Sbosticto save all or part of the pattern space for subsequent retrieval.
10055999Sbostic.Sh "Sed Addresses"
10155999SbosticAn address is not required, but if specified must be a number (that counts
10255999Sbosticinput lines
10355999Sbosticcumulatively across input files), a dollar
10455999Sbostic.Po
10555999Sbostic.Dq $
10655999Sbostic.Pc
10755999Sbosticcharacter that addresses the last line of input, or a context address
10855999Sbostic(which consists of a regular expression preceded and followed by a
10956018Sbosticdelimiter).
11055999Sbostic.Pp
11155999SbosticA command line with no addresses selects every pattern space.
11255999Sbostic.Pp
11355999SbosticA command line with one address selects all of the pattern spaces
11455999Sbosticthat match the address.
11555999Sbostic.Pp
11655999SbosticA command line with two addresses selects the inclusive range from
11755999Sbosticthe first pattern space that matches the first address through the next
11855999Sbosticpattern space that matches the second.
11955999Sbostic(If the second address is a number less than or equal to the line number
12055999Sbosticfirst selected, only that line is selected.)
12155999SbosticStarting at the first line following the selected range,
12255999Sbostic.Nm sed
12355999Sbosticstarts looking again for the first address.
12455999Sbostic.Pp
12555999SbosticEditing commands can be applied to non-selected pattern spaces by use
12655999Sbosticof the exclamation character
12755999Sbostic.Po
12855999Sbostic.Dq !
12955999Sbostic.Pc
13055999Sbosticfunction.
13155999Sbostic.Sh "Sed Regular Expressions"
13255999SbosticThe
13355999Sbostic.Nm sed
13455999Sbosticregular expressions are basic regular expressions (BRE's, see
13555999Sbostic.Xr regex 3
13655999Sbosticfor more information).
13755999SbosticIn addition,
13855999Sbostic.Nm sed
13955999Sbostichas the following two additions to BRE's:
14055999Sbostic.sp
14155999Sbostic.Bl -enum -compact
14255999Sbostic.It
14355999SbosticIn a context address, any character other than a backslash
14455999Sbostic.Po
14555999Sbostic.Dq \e
14655999Sbostic.Pc
14755999Sbosticor newline character may be used to delimit the regular expression.
14855999SbosticAlso, putting a backslash character before the delimiting character
14955999Sbosticcauses the character to be treated literally.
15055999SbosticFor example, in the context address \exabc\exdefx, the RE delimiter
15155999Sbosticis an
15255999Sbostic.Dq x
15355999Sbosticand the second
15455999Sbostic.Dq x
15555999Sbosticstands for itself, so that the regular expression is
15655999Sbostic.Dq abcxdef .
15755999Sbostic.sp
15855999Sbostic.It
15955999SbosticThe escape sequence \en matches a newline character embedded in the
16055999Sbosticpattern space.
16155999SbosticYou can't, however, use a literal newline character in an address or
16255999Sbosticin the substitute command.
16356018Sbostic.El
16456018Sbostic.Pp
16556018SbosticOne special feature of
16656018Sbostic.Nm sed
16756018Sbosticregular expressions is that they can default to the last regular
16856018Sbosticexpression used.
16956018SbosticIf a regular expression is empty, i.e. just the delimiter characters
17056018Sbosticare specified, the last regular expression encountered is used instead.
17156018SbosticThe last regular expression is defined as the last regular expression
17256018Sbosticused as part of an address or substitute command, and at run-time, not
17356018Sbosticcompile-time.
17456018SbosticFor example, the command
17556018Sbostic.Dq /abc/s//XXX/
17656018Sbosticwill substitute
17756018Sbostic.Dq XXX
17856018Sbosticfor the pattern
17956018Sbostic.Dq abc .
18055999Sbostic.Sh "Sed Functions"
18155999SbosticIn the following list of commands, the maximum number of permissible
18255999Sbosticaddresses for each command is indicated by [0addr], [1addr], or [2addr],
18355999Sbosticrepresenting zero, one, or two addresses.
18455999Sbostic.Pp
18555999SbosticThe argument
18655999Sbostic.Em text
18755999Sbosticconsists of one or more lines.
18855999SbosticTo embed a newline in the text, precede it with a backslash.
18955999SbosticOther backslashes in text are deleted and the following character
19055999Sbostictaken literally.
19155999Sbostic.Pp
19255999SbosticThe
19355999Sbostic.Dq r
19455999Sbosticand
19555999Sbostic.Dq w
19655999Sbosticfunctions take an optional file parameter, which should be separated
19755999Sbosticfrom the function letter by white space.
19855999SbosticEach file given as an argument to
19955999Sbostic.Nm sed
20055999Sbosticis created (or its contents truncated) before any input processing begins.
20155999Sbostic.Pp
20255999SbosticThe
20355999Sbostic.Dq b ,
20455999Sbostic.Dq r ,
20555999Sbostic.Dq s ,
20655999Sbostic.Dq t ,
20755999Sbostic.Dq w ,
20855999Sbostic.Dq y ,
20955999Sbostic.Dq ! ,
21055999Sbosticand
21155999Sbostic.Dq \&:
21255999Sbosticfunctions all accept additional arguments.
21355999SbosticThe following synopses indicate which arguments have to be separated from
21455999Sbosticthe function letters by white space characters.
21555999Sbostic.Pp
21655999SbosticTwo of the functions take a function-list.
21755999SbosticThis is a list of
21855999Sbostic.Nm sed
21955999Sbosticfunctions separated by newlines, as follows:
22055999Sbostic.Bd -literal -offset indent
22155999Sbostic{ function
22255999Sbostic  function
22355999Sbostic  ...
22455999Sbostic  function
22555999Sbostic}
22655999Sbostic.Ed
22755999Sbostic.Pp
22855999SbosticThe
22955999Sbostic.Dq {
23055999Sbosticcan be preceded by white space and can be followed by white space.
23155999SbosticThe function can be preceded by white space.
23255999SbosticThe terminating
23355999Sbostic.Dq }
234*65227Smckusickmust be preceded by a newline or optional white space.
23555999Sbostic.sp
23655999Sbostic.Bl -tag -width "XXXXXX" -compact
23755999Sbostic.It [2addr] function-list
23855999SbosticExecute function-list only when the pattern space is selected.
23955999Sbostic.sp
24055999Sbostic.It [1addr]a\e
24155999Sbostic.It text
24255999Sbostic.br
24355999SbosticWrite
24455999Sbostic.Em text
24555999Sbosticto standard output immediately before each attempt to read a line of input,
24655999Sbosticwhether by executing the
24755999Sbostic.Dq N
24855999Sbosticfunction or by beginning a new cycle.
24955999Sbostic.sp
25055999Sbostic.It [2addr]b[lable]
25155999SbosticBranch to the
25255999Sbostic.Dq \&:
25355999Sbosticfunction with the specified label.
25455999SbosticIf the label is not specified, branch to the end of the script.
25555999Sbostic.sp
25655999Sbostic.It [2addr]c\e
25755999Sbostic.It text
25855999Sbostic.br
25955999SbosticDelete the pattern space.
26055999SbosticWith 0 or 1 address or at the end of a 2-address range,
26155999Sbostic.Em text
26255999Sbosticis written to the standard output.
26355999Sbostic.sp
26455999Sbostic.It [2addr]d
26555999SbosticDelete the pattern space and start the next cycle.
26655999Sbostic.sp
26755999Sbostic.It [2addr]D
26855999SbosticDelete the initial segment of the pattern space through the first
26955999Sbosticnewline character and start the next cycle.
27055999Sbostic.sp
27155999Sbostic.It [2addr]g
27255999SbosticReplace the contents of the pattern space with the contents of the
27355999Sbostichold space.
27455999Sbostic.sp
27555999Sbostic.It [2addr]G
27655999SbosticAppend a newline character followed by the contents of the hold space
27755999Sbosticto the pattern space.
27855999Sbostic.sp
27955999Sbostic.It [2addr]h
28055999SbosticReplace the contents of the hold space with the contents of the
28155999Sbosticpattern space.
28255999Sbostic.sp
28355999Sbostic.It [2addr]H
28455999SbosticAppend a newline character followed by the contents of the pattern space
28555999Sbosticto the hold space.
28655999Sbostic.sp
28755999Sbostic.It [1addr]i\e
28855999Sbostic.It text
28955999Sbostic.br
29055999SbosticWrite
29155999Sbostic.Em text
29255999Sbosticto the standard output.
29355999Sbostic.sp
29455999Sbostic.It [2addr]l
29555999Sbostic(The letter ell.)
29655999SbosticWrite the pattern space to the standard output in a visually unambiguous
29755999Sbosticform.
29855999SbosticThis form is as follows:
29955999Sbostic.sp
30055999Sbostic.Bl -tag -width "carriage-returnXX" -offset indent -compact
30155999Sbostic.It backslash
30255999Sbostic\e
30355999Sbostic.It alert
30455999Sbostic\ea
30555999Sbostic.It form-feed
30655999Sbostic\ef
30755999Sbostic.It newline
30855999Sbostic\en
30955999Sbostic.It carriage-return
31055999Sbostic\er
31155999Sbostic.It tab
31255999Sbostic\et
31355999Sbostic.It vertical tab
31455999Sbostic\ev
31555999Sbostic.El
31655999Sbostic.Pp
31755999SbosticNonprintable characters are written as three-digit octal numbers (with a
31855999Sbosticpreceding backslash) for each byte in the character (most significant byte
31955999Sbosticfirst).
32055999SbosticLong lines are folded, with the point of folding indicated by displaying
32155999Sbostica backslash followed by a newline.
32255999SbosticThe end of each line is marked with a
32355999Sbostic.Dq $ .
32455999Sbostic.sp
32555999Sbostic.It [2addr]n
32655999SbosticWrite the pattern space to the standard output if the default output has
32755999Sbosticnot been suppressed, and replace the pattern space with the next line of
32855999Sbosticinput.
32955999Sbostic.sp
33055999Sbostic.It [2addr]N
33155999SbosticAppend the next line of input to the pattern space, using an embedded
33255999Sbosticnewline character to separate the appended material from the original
33355999Sbosticcontents.
33455999SbosticNote that the current line number changes.
33555999Sbostic.sp
33655999Sbostic.It [2addr]p
33755999SbosticWrite the pattern space to standard output.
33855999Sbostic.sp
33955999Sbostic.It [2addr]P
34055999SbosticWrite the pattern space, up to the first newline character to the
34155999Sbosticstandard output.
34255999Sbostic.sp
34355999Sbostic.It [1addr]q
34455999SbosticBranch to the end of the script and quit without starting a new cycle.
34555999Sbostic.sp
34655999Sbostic.It [1addr]r file
34755999SbosticCopy the contents of
34855999Sbostic.Em file
34955999Sbosticto the standard output immediately before the next attempt to read a
35055999Sbosticline of input.
35155999SbosticIf
35255999Sbostic.Em file
35355999Sbosticcannot be read for any reason, it is silently ignored and no error
35455999Sbosticcondition is set.
35555999Sbostic.sp
35655999Sbostic.It [2addr]s/regular expression/replacement/flags
35755999SbosticSubstitute the replacement string for the first instance of the regular
35855999Sbosticexpression in the pattern space.
35955999SbosticAny character other than backslash or newline can be used instead of
36055999Sbostica slash to delimit the RE and the replacement.
36155999SbosticWithin the RE and the replacement, the RE delimiter itself can be used as
36255999Sbostica literal character if it is preceded by a backslash.
36355999Sbostic.Pp
36455999SbosticAn ampersand
36555999Sbostic.Po
36655999Sbostic.Dq &
36755999Sbostic.Pc
36855999Sbosticappearing in the replacement is replaced by the string matching the RE.
36955999SbosticThe special meaning of
37055999Sbostic.Dq &
371*65227Smckusickin this context can be suppressed by preceding it by a backslash.
37255999SbosticThe string
37355999Sbostic.Dq \e# ,
37455999Sbosticwhere
37555999Sbostic.Dq #
37655999Sbosticis a digit, is replaced by the text matched
37755999Sbosticby the corresponding backreference expression (see
37855999Sbostic.Xr re_format 7 ).
37955999Sbostic.Pp
38055999SbosticA line can be split by substituting a newline character into it.
38155999SbosticTo specify a newline character in the replacement string, precede it with
38255999Sbostica backslash.
38355999Sbostic.Pp
38455999SbosticThe value of
38555999Sbostic.Em flags
38655999Sbosticin the substitute function is zero or more of the following:
38755999Sbostic.Bl -tag -width "XXXXXX" -offset indent
38855999Sbostic.It "0 ... 9"
38955999SbosticMake the substitution only for the N'th occurrence of the regular
39055999Sbosticexpression in the pattern space.
39155999Sbostic.It g
39255999SbosticMake the substitution for all non-overlapping matches of the
39355999Sbosticregular expression, not just the first one.
39455999Sbostic.It p
39555999SbosticWrite the pattern space to standard output if a replacement was made.
39655999SbosticIf the replacement string is identical to that which it replaces, it
39755999Sbosticis still considered to have been a replacement.
39855999Sbostic.It w Em file
39955999SbosticAppend the pattern space to
40055999Sbostic.Em file
40155999Sbosticif a replacement was made.
40255999SbosticIf the replacement string is identical to that which it replaces, it
40355999Sbosticis still considered to have been a replacement.
40455999Sbostic.El
40555999Sbostic.sp
40655999Sbostic.It [2addr]t [label]
40755999SbosticBranch to the
40855999Sbostic.Dq :
40955999Sbosticfunction bearing the label if any substitutions have been made since the
41055999Sbosticmost recent reading of an input line or execution of a
41155999Sbostic.Dq t
41255999Sbosticfunction.
41355999SbosticIf no label is specified, branch to the end of the script.
41455999Sbostic.sp
41555999Sbostic.It [2addr]w Em file
41655999SbosticAppend the pattern space to the
41755999Sbostic.Em file .
41855999Sbostic.sp
41955999Sbostic.It [2addr]x
42055999SbosticSwap the contents of the pattern and hold spaces.
42155999Sbostic.sp
42255999Sbostic.It [2addr]y/string1/string2/
42355999SbosticReplace all occurrences of characters in
42455999Sbostic.Em string1
42555999Sbosticin the pattern space with the corresponding characters from
42655999Sbostic.Em string2 .
42755999SbosticAny character other than a backslash or newline can be used instead of
42855999Sbostica slash to delimit the strings.
42955999SbosticWithin
43055999Sbostic.Em string1
43155999Sbosticand
43255999Sbostic.Em string2 ,
43358538Sbostica backslash followed by any character other than a newline is that literal
43458538Sbosticcharacter, and a backslash followed by an ``n'' is replaced by a newline
43558538Sbosticcharacter.
43655999Sbostic.sp
43755999Sbostic.It [2addr]!function
43855999Sbostic.It [2addr]!function-list
43955999SbosticApply the function or function-list only to the lines that are
44055999Sbostic.Em not
44155999Sbosticselected by the address(es).
44255999Sbostic.sp
44355999Sbostic.It [0addr]:label
44455999SbosticThis function does nothing; it bears a label to which the
44555999Sbostic.Dq b
44655999Sbosticand
44755999Sbostic.Dq t
44855999Sbosticcommands may branch.
44955999Sbostic.sp
45055999Sbostic.It [1addr]=
45155999SbosticWrite the line number to the standard output followed by a newline
45255999Sbosticcharacter.
45355999Sbostic.sp
45455999Sbostic.It [0addr]
45555999SbosticEmpty lines are ignored.
45655999Sbostic.sp
45755999Sbostic.It [0addr]#
45855999SbosticThe
45955999Sbostic.Dq #
46055999Sbosticand the remainder of the line are ignored (treated as a comment), with
46155999Sbosticthe single exception that if the first two characters in the file are
46255999Sbostic.Dq #n ,
46355999Sbosticthe default output is suppressed.
46455999SbosticThis is the same as specifying the
46555999Sbostic.Fl n
46655999Sbosticoption on the command line.
46755999Sbostic.El
46855999Sbostic.Pp
46955999SbosticThe
47055999Sbostic.Nm sed
47155999Sbosticutility exits 0 on success and >0 if an error occurs.
47255999Sbostic.Sh SEE ALSO
47355999Sbostic.Xr awk 1 ,
47455999Sbostic.Xr ed 1 ,
47555999Sbostic.Xr grep 1 ,
47655999Sbostic.Xr regex 3 ,
47755999Sbostic.Xr re_format 7
47855999Sbostic.Sh HISTORY
47955999SbosticA
48055999Sbostic.Nm sed
48155999Sbosticcommand appeared in
48255999Sbostic.At v7 .
48355999Sbostic.Sh STANDARDS
48455999SbosticThe
48555999Sbostic.Nm sed
48655999Sbosticfunction is expected to be a superset of the
48755999Sbostic.St -p1003.2
48855999Sbosticspecification.
489