xref: /freebsd/usr.bin/sed/sed.1 (revision 583bb9c530b2316c83017fc51517d3acad1ed9dd)
19b50d902SRodney W. Grimes.\" Copyright (c) 1992, 1993
29b50d902SRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
39b50d902SRodney W. Grimes.\"
49b50d902SRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
59b50d902SRodney W. Grimes.\" the Institute of Electrical and Electronics Engineers, Inc.
69b50d902SRodney W. Grimes.\"
79b50d902SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes.\" modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes.\" are met:
109b50d902SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes.\"    without specific prior written permission.
189b50d902SRodney W. Grimes.\"
199b50d902SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes.\" SUCH DAMAGE.
309b50d902SRodney W. Grimes.\"
319b50d902SRodney W. Grimes.\"	@(#)sed.1	8.2 (Berkeley) 12/30/93
32c3aac50fSPeter Wemm.\" $FreeBSD$
339b50d902SRodney W. Grimes.\"
349048d9a9SGordon Bergling.Dd April 8, 2021
359b50d902SRodney W. Grimes.Dt SED 1
369b50d902SRodney W. Grimes.Os
379b50d902SRodney W. Grimes.Sh NAME
389b50d902SRodney W. Grimes.Nm sed
399b50d902SRodney W. Grimes.Nd stream editor
409b50d902SRodney W. Grimes.Sh SYNOPSIS
418fe908efSRuslan Ermilov.Nm
42fc39ce9eSPedro F. Giffuni.Op Fl Ealnru
439b50d902SRodney W. Grimes.Ar command
448655a965SBenedict Reuschling.Op Fl I Ar extension
458655a965SBenedict Reuschling.Op Fl i Ar extension
4647dec781SRuslan Ermilov.Op Ar
478fe908efSRuslan Ermilov.Nm
488655a965SBenedict Reuschling.Op Fl Ealnru
499b50d902SRodney W. Grimes.Op Fl e Ar command
509b50d902SRodney W. Grimes.Op Fl f Ar command_file
51f6703c9cSYaroslav Tykhiy.Op Fl I Ar extension
52839af0c1SJuli Mallett.Op Fl i Ar extension
5347dec781SRuslan Ermilov.Op Ar
549b50d902SRodney W. Grimes.Sh DESCRIPTION
559b50d902SRodney W. GrimesThe
5673a08bb2SPhilippe Charnier.Nm
579b50d902SRodney W. Grimesutility reads the specified files, or the standard input if no files
589b50d902SRodney W. Grimesare specified, modifying the input as specified by a list of commands.
599b50d902SRodney W. GrimesThe input is then written to the standard output.
609b50d902SRodney W. Grimes.Pp
619b50d902SRodney W. GrimesA single command may be specified as the first argument to
628fe908efSRuslan Ermilov.Nm .
639b50d902SRodney W. GrimesMultiple commands may be specified by using the
649b50d902SRodney W. Grimes.Fl e
659b50d902SRodney W. Grimesor
669b50d902SRodney W. Grimes.Fl f
679b50d902SRodney W. Grimesoptions.
689b50d902SRodney W. GrimesAll commands are applied to the input in the order they are specified
699b50d902SRodney W. Grimesregardless of their origin.
709b50d902SRodney W. Grimes.Pp
719b50d902SRodney W. GrimesThe following options are available:
729b50d902SRodney W. Grimes.Bl -tag -width indent
73175de1e6SBrian Feldman.It Fl E
74175de1e6SBrian FeldmanInterpret regular expressions as extended (modern) regular expressions
750ef1d1f5SBrian Feldmanrather than basic regular expressions (BRE's).
760ef1d1f5SBrian FeldmanThe
77175de1e6SBrian Feldman.Xr re_format 7
78175de1e6SBrian Feldmanmanual page fully describes both formats.
799b50d902SRodney W. Grimes.It Fl a
809b50d902SRodney W. GrimesThe files listed as parameters for the
819b50d902SRodney W. Grimes.Dq w
829b50d902SRodney W. Grimesfunctions are created (or truncated) before any processing begins,
839b50d902SRodney W. Grimesby default.
849b50d902SRodney W. GrimesThe
859b50d902SRodney W. Grimes.Fl a
869b50d902SRodney W. Grimesoption causes
8773a08bb2SPhilippe Charnier.Nm
889b50d902SRodney W. Grimesto delay opening each file until a command containing the related
899b50d902SRodney W. Grimes.Dq w
909b50d902SRodney W. Grimesfunction is applied to a line of input.
919b50d902SRodney W. Grimes.It Fl e Ar command
929b50d902SRodney W. GrimesAppend the editing commands specified by the
939b50d902SRodney W. Grimes.Ar command
949b50d902SRodney W. Grimesargument
959b50d902SRodney W. Grimesto the list of commands.
969b50d902SRodney W. Grimes.It Fl f Ar command_file
979b50d902SRodney W. GrimesAppend the editing commands found in the file
989b50d902SRodney W. Grimes.Ar command_file
999b50d902SRodney W. Grimesto the list of commands.
1009b50d902SRodney W. GrimesThe editing commands should each be listed on a separate line.
101f9ab72bbSMateusz PiotrowskiThe commands are read from the standard input if
102f9ab72bbSMateusz Piotrowski.Ar command_file
103f9ab72bbSMateusz Piotrowskiis
104f9ab72bbSMateusz Piotrowski.Dq Li - .
105f6703c9cSYaroslav Tykhiy.It Fl I Ar extension
106839af0c1SJuli MallettEdit files in-place, saving backups with the specified
107839af0c1SJuli Mallett.Ar extension .
1085d16412dSJuli MallettIf a zero-length
1095d16412dSJuli Mallett.Ar extension
1105d16412dSJuli Mallettis given, no backup will be saved.
1115d16412dSJuli MallettIt is not recommended to give a zero-length
1125d16412dSJuli Mallett.Ar extension
1135d16412dSJuli Mallettwhen in-place editing files, as you risk corruption or partial content
1145d16412dSJuli Mallettin situations where disk space is exhausted, etc.
115f6703c9cSYaroslav Tykhiy.Pp
116f6703c9cSYaroslav TykhiyNote that in-place editing with
117f6703c9cSYaroslav Tykhiy.Fl I
118f6703c9cSYaroslav Tykhiystill takes place in a single continuous line address space covering
119f6703c9cSYaroslav Tykhiyall files, although each file preserves its individuality instead of
120f6703c9cSYaroslav Tykhiyforming one output stream.
121f6703c9cSYaroslav TykhiyThe line counter is never reset between files, address ranges can span
122f6703c9cSYaroslav Tykhiyfile boundaries, and the
123f6703c9cSYaroslav Tykhiy.Dq $
124f6703c9cSYaroslav Tykhiyaddress matches only the last line of the last file.
125f6703c9cSYaroslav Tykhiy(See
126f6703c9cSYaroslav Tykhiy.Sx "Sed Addresses" . )
127f6703c9cSYaroslav TykhiyThat can lead to unexpected results in many cases of in-place editing,
128f6703c9cSYaroslav Tykhiywhere using
129f6703c9cSYaroslav Tykhiy.Fl i
130f6703c9cSYaroslav Tykhiyis desired.
131f6703c9cSYaroslav Tykhiy.It Fl i Ar extension
132f6703c9cSYaroslav TykhiyEdit files in-place similarly to
133f6703c9cSYaroslav Tykhiy.Fl I ,
134f6703c9cSYaroslav Tykhiybut treat each file independently from other files.
135f6703c9cSYaroslav TykhiyIn particular, line numbers in each file start at 1,
136f6703c9cSYaroslav Tykhiythe
137f6703c9cSYaroslav Tykhiy.Dq $
138f6703c9cSYaroslav Tykhiyaddress matches the last line of the current file,
139f6703c9cSYaroslav Tykhiyand address ranges are limited to the current file.
140f6703c9cSYaroslav Tykhiy(See
141f6703c9cSYaroslav Tykhiy.Sx "Sed Addresses" . )
142f6703c9cSYaroslav TykhiyThe net result is as though each file were edited by a separate
143f6703c9cSYaroslav Tykhiy.Nm
144f6703c9cSYaroslav Tykhiyinstance.
1451ed86f6cSGleb Smirnoff.It Fl l
1461ed86f6cSGleb SmirnoffMake output line buffered.
1479b50d902SRodney W. Grimes.It Fl n
1489b50d902SRodney W. GrimesBy default, each line of input is echoed to the standard output after
1499b50d902SRodney W. Grimesall of the commands have been applied to it.
1509b50d902SRodney W. GrimesThe
1519b50d902SRodney W. Grimes.Fl n
1529b50d902SRodney W. Grimesoption suppresses this behavior.
153701d73b6SWarner Losh.It Fl r
154701d73b6SWarner LoshSame as
155701d73b6SWarner Losh.Fl E
156701d73b6SWarner Loshfor compatibility with GNU sed.
157fc39ce9eSPedro F. Giffuni.It Fl u
158fc39ce9eSPedro F. GiffuniMake output unbuffered.
1599b50d902SRodney W. Grimes.El
1609b50d902SRodney W. Grimes.Pp
1619b50d902SRodney W. GrimesThe form of a
16273a08bb2SPhilippe Charnier.Nm
1639b50d902SRodney W. Grimescommand is as follows:
164726b61abSRuslan Ermilov.Pp
1659b50d902SRodney W. Grimes.Dl [address[,address]]function[arguments]
166726b61abSRuslan Ermilov.Pp
1679b50d902SRodney W. GrimesWhitespace may be inserted before the first address and the function
1689b50d902SRodney W. Grimesportions of the command.
1699b50d902SRodney W. Grimes.Pp
1709b50d902SRodney W. GrimesNormally,
17173a08bb2SPhilippe Charnier.Nm
1729b50d902SRodney W. Grimescyclically copies a line of input, not including its terminating newline
1739b50d902SRodney W. Grimescharacter, into a
1749b50d902SRodney W. Grimes.Em "pattern space" ,
1759b50d902SRodney W. Grimes(unless there is something left after a
1769b50d902SRodney W. Grimes.Dq D
1779b50d902SRodney W. Grimesfunction),
1789b50d902SRodney W. Grimesapplies all of the commands with addresses that select that pattern space,
1799b50d902SRodney W. Grimescopies the pattern space to the standard output, appending a newline, and
1809b50d902SRodney W. Grimesdeletes the pattern space.
1819b50d902SRodney W. Grimes.Pp
1829b50d902SRodney W. GrimesSome of the functions use a
1839b50d902SRodney W. Grimes.Em "hold space"
1849b50d902SRodney W. Grimesto save all or part of the pattern space for subsequent retrieval.
1859b50d902SRodney W. Grimes.Sh "Sed Addresses"
186f6703c9cSYaroslav TykhiyAn address is not required, but if specified must have one of the
187f6703c9cSYaroslav Tykhiyfollowing formats:
188f6703c9cSYaroslav Tykhiy.Bl -bullet -offset indent
189f6703c9cSYaroslav Tykhiy.It
190f6703c9cSYaroslav Tykhiya number that counts
1919b50d902SRodney W. Grimesinput lines
192f6703c9cSYaroslav Tykhiycumulatively across input files (or in each file independently
193f6703c9cSYaroslav Tykhiyif a
194f6703c9cSYaroslav Tykhiy.Fl i
195f6703c9cSYaroslav Tykhiyoption is in effect);
196f6703c9cSYaroslav Tykhiy.It
197f6703c9cSYaroslav Tykhiya dollar
198c4d9468eSRuslan Ermilov.Pq Dq $
199f6703c9cSYaroslav Tykhiycharacter that addresses the last line of input (or the last line
200f6703c9cSYaroslav Tykhiyof the current file if a
201f6703c9cSYaroslav Tykhiy.Fl i
202f6703c9cSYaroslav Tykhiyoption was specified);
203f6703c9cSYaroslav Tykhiy.It
204f6703c9cSYaroslav Tykhiya context address
205f6703c9cSYaroslav Tykhiythat consists of a regular expression preceded and followed by a
2068655a965SBenedict Reuschlingdelimiter.
2078655a965SBenedict ReuschlingThe closing delimiter can also optionally be followed by the
2086351d609SPedro F. Giffuni.Dq I
209bdd72b70SSuleiman Souhlalcharacter, to indicate that the regular expression is to be matched
210bdd72b70SSuleiman Souhlalin a case-insensitive way.
211f6703c9cSYaroslav Tykhiy.El
2129b50d902SRodney W. Grimes.Pp
2139b50d902SRodney W. GrimesA command line with no addresses selects every pattern space.
2149b50d902SRodney W. Grimes.Pp
2159b50d902SRodney W. GrimesA command line with one address selects all of the pattern spaces
2169b50d902SRodney W. Grimesthat match the address.
2179b50d902SRodney W. Grimes.Pp
2186a3e8b0aSRuslan ErmilovA command line with two addresses selects an inclusive range.
2196a3e8b0aSRuslan ErmilovThis
2201d2ed2f7SBill Swinglerange starts with the first pattern space that matches the first
2216a3e8b0aSRuslan Ermilovaddress.
2226a3e8b0aSRuslan ErmilovThe end of the range is the next following pattern space
2236a3e8b0aSRuslan Ermilovthat matches the second address.
2246a3e8b0aSRuslan ErmilovIf the second address is a number
2251d2ed2f7SBill Swingleless than or equal to the line number first selected, only that
2266a3e8b0aSRuslan Ermilovline is selected.
227f879e8d9SBrian SomersThe number in the second address may be prefixed with a
228f879e8d9SBrian Somers.Pq Dq \&+
229f879e8d9SBrian Somersto specify the number of lines to match after the first pattern.
2306a3e8b0aSRuslan ErmilovIn the case when the second address is a context
231b6059c5cSDiomidis Spinellisaddress,
232b6059c5cSDiomidis Spinellis.Nm
233b6059c5cSDiomidis Spinellisdoes not re-match the second address against the
2346a3e8b0aSRuslan Ermilovpattern space that matched the first address.
2356a3e8b0aSRuslan ErmilovStarting at the
2362642bed2SGiorgos Keramidasfirst line following the selected range,
23773a08bb2SPhilippe Charnier.Nm
2389b50d902SRodney W. Grimesstarts looking again for the first address.
2399b50d902SRodney W. Grimes.Pp
2409b50d902SRodney W. GrimesEditing commands can be applied to non-selected pattern spaces by use
2419b50d902SRodney W. Grimesof the exclamation character
242c4d9468eSRuslan Ermilov.Pq Dq \&!
2439b50d902SRodney W. Grimesfunction.
2449b50d902SRodney W. Grimes.Sh "Sed Regular Expressions"
2450ef1d1f5SBrian FeldmanThe regular expressions used in
2468fe908efSRuslan Ermilov.Nm ,
2470ef1d1f5SBrian Feldmanby default, are basic regular expressions (BREs, see
2480ef1d1f5SBrian Feldman.Xr re_format 7
249ca802a8aSPhilippe Charnierfor more information), but extended (modern) regular expressions can be used
250ca802a8aSPhilippe Charnierinstead if the
251175de1e6SBrian Feldman.Fl E
2520ef1d1f5SBrian Feldmanflag is given.
2530ef1d1f5SBrian FeldmanIn addition,
254175de1e6SBrian Feldman.Nm
255175de1e6SBrian Feldmanhas the following two additions to regular expressions:
256726b61abSRuslan Ermilov.Pp
2579b50d902SRodney W. Grimes.Bl -enum -compact
2589b50d902SRodney W. Grimes.It
2599b50d902SRodney W. GrimesIn a context address, any character other than a backslash
260c4d9468eSRuslan Ermilov.Pq Dq \e
2619b50d902SRodney W. Grimesor newline character may be used to delimit the regular expression.
262625451f9SYaroslav TykhiyThe opening delimiter needs to be preceded by a backslash
263625451f9SYaroslav Tykhiyunless it is a slash.
264625451f9SYaroslav TykhiyFor example, the context address
265625451f9SYaroslav Tykhiy.Li \exabcx
266625451f9SYaroslav Tykhiyis equivalent to
267625451f9SYaroslav Tykhiy.Li /abc/ .
2689b50d902SRodney W. GrimesAlso, putting a backslash character before the delimiting character
269db6fc083SYaroslav Tykhiywithin the regular expression causes the character to be treated literally.
270625451f9SYaroslav TykhiyFor example, in the context address
271625451f9SYaroslav Tykhiy.Li \exabc\exdefx ,
272625451f9SYaroslav Tykhiythe RE delimiter is an
2739b50d902SRodney W. Grimes.Dq x
2749b50d902SRodney W. Grimesand the second
2759b50d902SRodney W. Grimes.Dq x
2769b50d902SRodney W. Grimesstands for itself, so that the regular expression is
2779b50d902SRodney W. Grimes.Dq abcxdef .
278726b61abSRuslan Ermilov.Pp
2799b50d902SRodney W. Grimes.It
2809b50d902SRodney W. GrimesThe escape sequence \en matches a newline character embedded in the
2819b50d902SRodney W. Grimespattern space.
2820227791bSRuslan ErmilovYou cannot, however, use a literal newline character in an address or
2839b50d902SRodney W. Grimesin the substitute command.
2849b50d902SRodney W. Grimes.El
2859b50d902SRodney W. Grimes.Pp
2869b50d902SRodney W. GrimesOne special feature of
28773a08bb2SPhilippe Charnier.Nm
2889b50d902SRodney W. Grimesregular expressions is that they can default to the last regular
2899b50d902SRodney W. Grimesexpression used.
2906a3e8b0aSRuslan ErmilovIf a regular expression is empty, i.e., just the delimiter characters
2919b50d902SRodney W. Grimesare specified, the last regular expression encountered is used instead.
2929b50d902SRodney W. GrimesThe last regular expression is defined as the last regular expression
2939b50d902SRodney W. Grimesused as part of an address or substitute command, and at run-time, not
2949b50d902SRodney W. Grimescompile-time.
2959b50d902SRodney W. GrimesFor example, the command
2969b50d902SRodney W. Grimes.Dq /abc/s//XXX/
2979b50d902SRodney W. Grimeswill substitute
2989b50d902SRodney W. Grimes.Dq XXX
2999b50d902SRodney W. Grimesfor the pattern
3009b50d902SRodney W. Grimes.Dq abc .
3019b50d902SRodney W. Grimes.Sh "Sed Functions"
3029b50d902SRodney W. GrimesIn the following list of commands, the maximum number of permissible
3039b50d902SRodney W. Grimesaddresses for each command is indicated by [0addr], [1addr], or [2addr],
3049b50d902SRodney W. Grimesrepresenting zero, one, or two addresses.
3059b50d902SRodney W. Grimes.Pp
3069b50d902SRodney W. GrimesThe argument
3079b50d902SRodney W. Grimes.Em text
3089b50d902SRodney W. Grimesconsists of one or more lines.
3099b50d902SRodney W. GrimesTo embed a newline in the text, precede it with a backslash.
3109b50d902SRodney W. GrimesOther backslashes in text are deleted and the following character
3119b50d902SRodney W. Grimestaken literally.
3129b50d902SRodney W. Grimes.Pp
3139b50d902SRodney W. GrimesThe
3149b50d902SRodney W. Grimes.Dq r
3159b50d902SRodney W. Grimesand
3169b50d902SRodney W. Grimes.Dq w
3179b50d902SRodney W. Grimesfunctions take an optional file parameter, which should be separated
3189b50d902SRodney W. Grimesfrom the function letter by white space.
3199b50d902SRodney W. GrimesEach file given as an argument to
32073a08bb2SPhilippe Charnier.Nm
3219b50d902SRodney W. Grimesis created (or its contents truncated) before any input processing begins.
3229b50d902SRodney W. Grimes.Pp
3239b50d902SRodney W. GrimesThe
3249b50d902SRodney W. Grimes.Dq b ,
3259b50d902SRodney W. Grimes.Dq r ,
3269b50d902SRodney W. Grimes.Dq s ,
3279b50d902SRodney W. Grimes.Dq t ,
3289b50d902SRodney W. Grimes.Dq w ,
3299b50d902SRodney W. Grimes.Dq y ,
3309b88faecSRuslan Ermilov.Dq \&! ,
3319b50d902SRodney W. Grimesand
3329b50d902SRodney W. Grimes.Dq \&:
3339b50d902SRodney W. Grimesfunctions all accept additional arguments.
3349b50d902SRodney W. GrimesThe following synopses indicate which arguments have to be separated from
3359b50d902SRodney W. Grimesthe function letters by white space characters.
3369b50d902SRodney W. Grimes.Pp
3379b50d902SRodney W. GrimesTwo of the functions take a function-list.
3389b50d902SRodney W. GrimesThis is a list of
33973a08bb2SPhilippe Charnier.Nm
3409b50d902SRodney W. Grimesfunctions separated by newlines, as follows:
3419b50d902SRodney W. Grimes.Bd -literal -offset indent
3429b50d902SRodney W. Grimes{ function
3439b50d902SRodney W. Grimes  function
3449b50d902SRodney W. Grimes  ...
3459b50d902SRodney W. Grimes  function
3469b50d902SRodney W. Grimes}
3479b50d902SRodney W. Grimes.Ed
3489b50d902SRodney W. Grimes.Pp
3499b50d902SRodney W. GrimesThe
3509b50d902SRodney W. Grimes.Dq {
3519b50d902SRodney W. Grimescan be preceded by white space and can be followed by white space.
3529b50d902SRodney W. GrimesThe function can be preceded by white space.
3539b50d902SRodney W. GrimesThe terminating
3549b50d902SRodney W. Grimes.Dq }
3557d3c2941SAndrey A. Chernovmust be preceded by a newline, and may also be preceded by white space.
356726b61abSRuslan Ermilov.Pp
3579b50d902SRodney W. Grimes.Bl -tag -width "XXXXXX" -compact
3589b50d902SRodney W. Grimes.It [2addr] function-list
3599b50d902SRodney W. GrimesExecute function-list only when the pattern space is selected.
360726b61abSRuslan Ermilov.Pp
3619b50d902SRodney W. Grimes.It [1addr]a\e
3629b50d902SRodney W. Grimes.It text
3639b50d902SRodney W. GrimesWrite
3649b50d902SRodney W. Grimes.Em text
3659b50d902SRodney W. Grimesto standard output immediately before each attempt to read a line of input,
3669b50d902SRodney W. Grimeswhether by executing the
3679b50d902SRodney W. Grimes.Dq N
3689b50d902SRodney W. Grimesfunction or by beginning a new cycle.
369726b61abSRuslan Ermilov.Pp
37053daef24SAndreas Schulz.It [2addr]b[label]
3719b50d902SRodney W. GrimesBranch to the
3729b50d902SRodney W. Grimes.Dq \&:
3739b50d902SRodney W. Grimesfunction with the specified label.
3749b50d902SRodney W. GrimesIf the label is not specified, branch to the end of the script.
375726b61abSRuslan Ermilov.Pp
3769b50d902SRodney W. Grimes.It [2addr]c\e
3779b50d902SRodney W. Grimes.It text
3789b50d902SRodney W. GrimesDelete the pattern space.
3799b50d902SRodney W. GrimesWith 0 or 1 address or at the end of a 2-address range,
3809b50d902SRodney W. Grimes.Em text
3819b50d902SRodney W. Grimesis written to the standard output.
382726b61abSRuslan Ermilov.Pp
3839b50d902SRodney W. Grimes.It [2addr]d
3849b50d902SRodney W. GrimesDelete the pattern space and start the next cycle.
385726b61abSRuslan Ermilov.Pp
3869b50d902SRodney W. Grimes.It [2addr]D
3879b50d902SRodney W. GrimesDelete the initial segment of the pattern space through the first
3889b50d902SRodney W. Grimesnewline character and start the next cycle.
389726b61abSRuslan Ermilov.Pp
3909b50d902SRodney W. Grimes.It [2addr]g
3919b50d902SRodney W. GrimesReplace the contents of the pattern space with the contents of the
3929b50d902SRodney W. Grimeshold space.
393726b61abSRuslan Ermilov.Pp
3949b50d902SRodney W. Grimes.It [2addr]G
3959b50d902SRodney W. GrimesAppend a newline character followed by the contents of the hold space
3969b50d902SRodney W. Grimesto the pattern space.
397726b61abSRuslan Ermilov.Pp
3989b50d902SRodney W. Grimes.It [2addr]h
3999b50d902SRodney W. GrimesReplace the contents of the hold space with the contents of the
4009b50d902SRodney W. Grimespattern space.
401726b61abSRuslan Ermilov.Pp
4029b50d902SRodney W. Grimes.It [2addr]H
4039b50d902SRodney W. GrimesAppend a newline character followed by the contents of the pattern space
4049b50d902SRodney W. Grimesto the hold space.
405726b61abSRuslan Ermilov.Pp
4069b50d902SRodney W. Grimes.It [1addr]i\e
4079b50d902SRodney W. Grimes.It text
4089b50d902SRodney W. GrimesWrite
4099b50d902SRodney W. Grimes.Em text
4109b50d902SRodney W. Grimesto the standard output.
411726b61abSRuslan Ermilov.Pp
4129b50d902SRodney W. Grimes.It [2addr]l
4139b50d902SRodney W. Grimes(The letter ell.)
4149b50d902SRodney W. GrimesWrite the pattern space to the standard output in a visually unambiguous
4159b50d902SRodney W. Grimesform.
4169b50d902SRodney W. GrimesThis form is as follows:
417726b61abSRuslan Ermilov.Pp
4189b50d902SRodney W. Grimes.Bl -tag -width "carriage-returnXX" -offset indent -compact
4199b50d902SRodney W. Grimes.It backslash
42007847cc8SMike Pritchard\e\e
4219b50d902SRodney W. Grimes.It alert
4229b50d902SRodney W. Grimes\ea
4239b50d902SRodney W. Grimes.It form-feed
4249b50d902SRodney W. Grimes\ef
4259b50d902SRodney W. Grimes.It carriage-return
4269b50d902SRodney W. Grimes\er
4279b50d902SRodney W. Grimes.It tab
4289b50d902SRodney W. Grimes\et
4299b50d902SRodney W. Grimes.It vertical tab
4309b50d902SRodney W. Grimes\ev
4319b50d902SRodney W. Grimes.El
4329b50d902SRodney W. Grimes.Pp
4339b50d902SRodney W. GrimesNonprintable characters are written as three-digit octal numbers (with a
4349b50d902SRodney W. Grimespreceding backslash) for each byte in the character (most significant byte
4359b50d902SRodney W. Grimesfirst).
4369b50d902SRodney W. GrimesLong lines are folded, with the point of folding indicated by displaying
4379b50d902SRodney W. Grimesa backslash followed by a newline.
4389b50d902SRodney W. GrimesThe end of each line is marked with a
4399b50d902SRodney W. Grimes.Dq $ .
440726b61abSRuslan Ermilov.Pp
4419b50d902SRodney W. Grimes.It [2addr]n
4429b50d902SRodney W. GrimesWrite the pattern space to the standard output if the default output has
4439b50d902SRodney W. Grimesnot been suppressed, and replace the pattern space with the next line of
4449b50d902SRodney W. Grimesinput.
445726b61abSRuslan Ermilov.Pp
4469b50d902SRodney W. Grimes.It [2addr]N
4479b50d902SRodney W. GrimesAppend the next line of input to the pattern space, using an embedded
4489b50d902SRodney W. Grimesnewline character to separate the appended material from the original
4499b50d902SRodney W. Grimescontents.
4509b50d902SRodney W. GrimesNote that the current line number changes.
451726b61abSRuslan Ermilov.Pp
4529b50d902SRodney W. Grimes.It [2addr]p
4539b50d902SRodney W. GrimesWrite the pattern space to standard output.
454726b61abSRuslan Ermilov.Pp
4559b50d902SRodney W. Grimes.It [2addr]P
4569b50d902SRodney W. GrimesWrite the pattern space, up to the first newline character to the
4579b50d902SRodney W. Grimesstandard output.
458726b61abSRuslan Ermilov.Pp
4599b50d902SRodney W. Grimes.It [1addr]q
4609b50d902SRodney W. GrimesBranch to the end of the script and quit without starting a new cycle.
461726b61abSRuslan Ermilov.Pp
4629b50d902SRodney W. Grimes.It [1addr]r file
4639b50d902SRodney W. GrimesCopy the contents of
4649b50d902SRodney W. Grimes.Em file
4659b50d902SRodney W. Grimesto the standard output immediately before the next attempt to read a
4669b50d902SRodney W. Grimesline of input.
4679b50d902SRodney W. GrimesIf
4689b50d902SRodney W. Grimes.Em file
4699b50d902SRodney W. Grimescannot be read for any reason, it is silently ignored and no error
4709b50d902SRodney W. Grimescondition is set.
471726b61abSRuslan Ermilov.Pp
4729b50d902SRodney W. Grimes.It [2addr]s/regular expression/replacement/flags
4739b50d902SRodney W. GrimesSubstitute the replacement string for the first instance of the regular
4749b50d902SRodney W. Grimesexpression in the pattern space.
4759b50d902SRodney W. GrimesAny character other than backslash or newline can be used instead of
4769b50d902SRodney W. Grimesa slash to delimit the RE and the replacement.
4779b50d902SRodney W. GrimesWithin the RE and the replacement, the RE delimiter itself can be used as
4789b50d902SRodney W. Grimesa literal character if it is preceded by a backslash.
4799b50d902SRodney W. Grimes.Pp
4809b50d902SRodney W. GrimesAn ampersand
481c4d9468eSRuslan Ermilov.Pq Dq &
4829b50d902SRodney W. Grimesappearing in the replacement is replaced by the string matching the RE.
4839b50d902SRodney W. GrimesThe special meaning of
4849b50d902SRodney W. Grimes.Dq &
4859b50d902SRodney W. Grimesin this context can be suppressed by preceding it by a backslash.
4869b50d902SRodney W. GrimesThe string
4879b50d902SRodney W. Grimes.Dq \e# ,
4889b50d902SRodney W. Grimeswhere
4899b50d902SRodney W. Grimes.Dq #
4909b50d902SRodney W. Grimesis a digit, is replaced by the text matched
4919b50d902SRodney W. Grimesby the corresponding backreference expression (see
4929b50d902SRodney W. Grimes.Xr re_format 7 ) .
4939b50d902SRodney W. Grimes.Pp
4949b50d902SRodney W. GrimesA line can be split by substituting a newline character into it.
4959b50d902SRodney W. GrimesTo specify a newline character in the replacement string, precede it with
4969b50d902SRodney W. Grimesa backslash.
4979b50d902SRodney W. Grimes.Pp
4989b50d902SRodney W. GrimesThe value of
4999b50d902SRodney W. Grimes.Em flags
5009b50d902SRodney W. Grimesin the substitute function is zero or more of the following:
5019b50d902SRodney W. Grimes.Bl -tag -width "XXXXXX" -offset indent
50206e482e6SRuslan Ermilov.It Ar N
5031fa600e8STim J. RobbinsMake the substitution only for the
50406e482e6SRuslan Ermilov.Ar N Ns 'th
5051fa600e8STim J. Robbinsoccurrence of the regular expression in the pattern space.
5069b50d902SRodney W. Grimes.It g
5079b50d902SRodney W. GrimesMake the substitution for all non-overlapping matches of the
5089b50d902SRodney W. Grimesregular expression, not just the first one.
5099b50d902SRodney W. Grimes.It p
5109b50d902SRodney W. GrimesWrite the pattern space to standard output if a replacement was made.
5119b50d902SRodney W. GrimesIf the replacement string is identical to that which it replaces, it
5129b50d902SRodney W. Grimesis still considered to have been a replacement.
5139b50d902SRodney W. Grimes.It w Em file
5149b50d902SRodney W. GrimesAppend the pattern space to
5159b50d902SRodney W. Grimes.Em file
5169b50d902SRodney W. Grimesif a replacement was made.
5179b50d902SRodney W. GrimesIf the replacement string is identical to that which it replaces, it
5189b50d902SRodney W. Grimesis still considered to have been a replacement.
51949e89014SEitan Adler.It i or I
520bdd72b70SSuleiman SouhlalMatch the regular expression in a case-insensitive way.
5219b50d902SRodney W. Grimes.El
522726b61abSRuslan Ermilov.Pp
5239b50d902SRodney W. Grimes.It [2addr]t [label]
5249b50d902SRodney W. GrimesBranch to the
5259edc38efSSteve Price.Dq \&:
5269b50d902SRodney W. Grimesfunction bearing the label if any substitutions have been made since the
5279b50d902SRodney W. Grimesmost recent reading of an input line or execution of a
5289b50d902SRodney W. Grimes.Dq t
5299b50d902SRodney W. Grimesfunction.
5309b50d902SRodney W. GrimesIf no label is specified, branch to the end of the script.
531726b61abSRuslan Ermilov.Pp
5329b50d902SRodney W. Grimes.It [2addr]w Em file
5339b50d902SRodney W. GrimesAppend the pattern space to the
5349b50d902SRodney W. Grimes.Em file .
535726b61abSRuslan Ermilov.Pp
5369b50d902SRodney W. Grimes.It [2addr]x
5379b50d902SRodney W. GrimesSwap the contents of the pattern and hold spaces.
538726b61abSRuslan Ermilov.Pp
5399b50d902SRodney W. Grimes.It [2addr]y/string1/string2/
5409b50d902SRodney W. GrimesReplace all occurrences of characters in
5419b50d902SRodney W. Grimes.Em string1
5429b50d902SRodney W. Grimesin the pattern space with the corresponding characters from
5439b50d902SRodney W. Grimes.Em string2 .
5449b50d902SRodney W. GrimesAny character other than a backslash or newline can be used instead of
5459b50d902SRodney W. Grimesa slash to delimit the strings.
5469b50d902SRodney W. GrimesWithin
5479b50d902SRodney W. Grimes.Em string1
5489b50d902SRodney W. Grimesand
5499b50d902SRodney W. Grimes.Em string2 ,
5509b50d902SRodney W. Grimesa backslash followed by any character other than a newline is that literal
5519b50d902SRodney W. Grimescharacter, and a backslash followed by an ``n'' is replaced by a newline
5529b50d902SRodney W. Grimescharacter.
553726b61abSRuslan Ermilov.Pp
5549b50d902SRodney W. Grimes.It [2addr]!function
5559b50d902SRodney W. Grimes.It [2addr]!function-list
5569b50d902SRodney W. GrimesApply the function or function-list only to the lines that are
5579b50d902SRodney W. Grimes.Em not
5589b50d902SRodney W. Grimesselected by the address(es).
559726b61abSRuslan Ermilov.Pp
5609b50d902SRodney W. Grimes.It [0addr]:label
5619b50d902SRodney W. GrimesThis function does nothing; it bears a label to which the
5629b50d902SRodney W. Grimes.Dq b
5639b50d902SRodney W. Grimesand
5649b50d902SRodney W. Grimes.Dq t
5659b50d902SRodney W. Grimescommands may branch.
566726b61abSRuslan Ermilov.Pp
5679b50d902SRodney W. Grimes.It [1addr]=
5689b50d902SRodney W. GrimesWrite the line number to the standard output followed by a newline
5699b50d902SRodney W. Grimescharacter.
570726b61abSRuslan Ermilov.Pp
5719b50d902SRodney W. Grimes.It [0addr]
5729b50d902SRodney W. GrimesEmpty lines are ignored.
573726b61abSRuslan Ermilov.Pp
5749b50d902SRodney W. Grimes.It [0addr]#
5759b50d902SRodney W. GrimesThe
5769b50d902SRodney W. Grimes.Dq #
5779b50d902SRodney W. Grimesand the remainder of the line are ignored (treated as a comment), with
5789b50d902SRodney W. Grimesthe single exception that if the first two characters in the file are
5799b50d902SRodney W. Grimes.Dq #n ,
5809b50d902SRodney W. Grimesthe default output is suppressed.
5819b50d902SRodney W. GrimesThis is the same as specifying the
5829b50d902SRodney W. Grimes.Fl n
5839b50d902SRodney W. Grimesoption on the command line.
5849b50d902SRodney W. Grimes.El
58530e9580eSTim J. Robbins.Sh ENVIRONMENT
58630e9580eSTim J. RobbinsThe
58706e482e6SRuslan Ermilov.Ev COLUMNS , LANG , LC_ALL , LC_CTYPE
588e7cc3a21STim J. Robbinsand
589e7cc3a21STim J. Robbins.Ev LC_COLLATE
590e7cc3a21STim J. Robbinsenvironment variables affect the execution of
59130e9580eSTim J. Robbins.Nm
592e7cc3a21STim J. Robbinsas described in
593e7cc3a21STim J. Robbins.Xr environ 7 .
594a866e170SRuslan Ermilov.Sh EXIT STATUS
595d628d776SRuslan Ermilov.Ex -std
596d09b527fSBrad Davis.Sh EXAMPLES
597d09b527fSBrad DavisReplace
598d09b527fSBrad Davis.Ql bar
599d09b527fSBrad Daviswith
600d09b527fSBrad Davis.Ql baz
601d09b527fSBrad Daviswhen piped from another command:
602d09b527fSBrad Davis.Bd -literal -offset indent
603d09b527fSBrad Davisecho "An alternate word, like bar, is sometimes used in examples." | sed 's/bar/baz/'
604d09b527fSBrad Davis.Ed
605d09b527fSBrad Davis.Pp
606d09b527fSBrad DavisUsing backlashes can sometimes be hard to read and follow:
607d09b527fSBrad Davis.Bd -literal -offset indent
608d09b527fSBrad Davisecho "/home/example" | sed  's/\\/home\\/example/\\/usr\\/local\\/example/'
609d09b527fSBrad Davis.Ed
610d09b527fSBrad Davis.Pp
611d09b527fSBrad DavisUsing a different separator can be handy when working with paths:
612d09b527fSBrad Davis.Bd -literal -offset indent
613d09b527fSBrad Davisecho "/home/example" | sed 's#/home/example#/usr/local/example#'
614d09b527fSBrad Davis.Ed
615d09b527fSBrad Davis.Pp
616*583bb9c5SGordon BerglingReplace all occurrences of
617d09b527fSBrad Davis.Ql foo
618d09b527fSBrad Daviswith
619d09b527fSBrad Davis.Ql bar
620d09b527fSBrad Davisin the file
621d09b527fSBrad Davis.Pa test.txt ,
622d09b527fSBrad Daviswithout creating a backup of the file:
623d09b527fSBrad Davis.Bd -literal -offset indent
6240ae98ab7SBenedict Reuschlingsed -i '' -e 's/foo/bar/g' test.txt
625d09b527fSBrad Davis.Ed
6269b50d902SRodney W. Grimes.Sh SEE ALSO
6279b50d902SRodney W. Grimes.Xr awk 1 ,
6289b50d902SRodney W. Grimes.Xr ed 1 ,
6299b50d902SRodney W. Grimes.Xr grep 1 ,
6309b50d902SRodney W. Grimes.Xr regex 3 ,
6319b50d902SRodney W. Grimes.Xr re_format 7
6329048d9a9SGordon Bergling.Rs
6339048d9a9SGordon Bergling.\" 4.4BSD USD:15
6349048d9a9SGordon Bergling.%A Lee E. McMahon
6359048d9a9SGordon Bergling.%I AT&T Bell Laboratories
6369048d9a9SGordon Bergling.%T SED \(em A Non-interactive Text Editor
6379048d9a9SGordon Bergling.%R Computing Science Technical Report
6389048d9a9SGordon Bergling.%N 77
6399048d9a9SGordon Bergling.%D January 1979
6409048d9a9SGordon Bergling.Re
6419b50d902SRodney W. Grimes.Sh STANDARDS
6429b50d902SRodney W. GrimesThe
64373a08bb2SPhilippe Charnier.Nm
64442676968STim J. Robbinsutility is expected to be a superset of the
6459b50d902SRodney W. Grimes.St -p1003.2
6469b50d902SRodney W. Grimesspecification.
647839af0c1SJuli Mallett.Pp
648839af0c1SJuli MallettThe
649f6703c9cSYaroslav Tykhiy.Fl E , I , a
650220d8b58STim J. Robbinsand
651839af0c1SJuli Mallett.Fl i
652f9ab72bbSMateusz Piotrowskioptions, the special meaning of
653f9ab72bbSMateusz Piotrowski.Fl f Cm - ,
654f9ab72bbSMateusz Piotrowskithe prefixing
655f879e8d9SBrian Somers.Dq \&+
656f879e8d9SBrian Somersin the second member of an address range,
657f879e8d9SBrian Somersas well as the
658bdd72b70SSuleiman Souhlal.Dq I
659bdd72b70SSuleiman Souhlalflag to the address regular expression and substitution command are
660bdd72b70SSuleiman Souhlalnon-standard
661839af0c1SJuli Mallett.Fx
662220d8b58STim J. Robbinsextensions and may not be available on other operating systems.
66342676968STim J. Robbins.Sh HISTORY
66442676968STim J. RobbinsA
66542676968STim J. Robbins.Nm
6666a3e8b0aSRuslan Ermilovcommand, written by
6676a3e8b0aSRuslan Ermilov.An L. E. McMahon ,
6686a3e8b0aSRuslan Ermilovappeared in
66942676968STim J. Robbins.At v7 .
670b6059c5cSDiomidis Spinellis.Sh AUTHORS
6712b7af31cSBaptiste Daroussin.An Diomidis D. Spinellis Aq Mt dds@FreeBSD.org
6724f45d811STim J. Robbins.Sh BUGS
6738b086367STim J. RobbinsMultibyte characters containing a byte with value 0x5C
6744438d91eSRuslan Ermilov.Tn ( ASCII
6754438d91eSRuslan Ermilov.Ql \e )
6768b086367STim J. Robbinsmay be incorrectly treated as line continuation characters in arguments to the
6778b086367STim J. Robbins.Dq a ,
6788b086367STim J. Robbins.Dq c
6798b086367STim J. Robbinsand
6808b086367STim J. Robbins.Dq i
6818b086367STim J. Robbinscommands.
682e4d0177fSTim J. RobbinsMultibyte characters cannot be used as delimiters with the
683e4d0177fSTim J. Robbins.Dq s
684e4d0177fSTim J. Robbinsand
685e4d0177fSTim J. Robbins.Dq y
686e4d0177fSTim J. Robbinscommands.
687