man-pages/man1/sed.1.html
2021-03-31 01:06:50 +01:00

573 lines
14 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SED</TITLE>
</HEAD><BODY>
<H1>SED</H1>
Section: User Commands (1)<BR>Updated: December 2018<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
sed - stream editor for filtering and transforming text
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>sed</B>
[<I>OPTION</I>]... <I>{script-only-if-no-other-script} </I>[<I>input-file</I>]...
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<I>Sed</I> is a stream editor.
A stream editor is used to perform basic text
transformations on an input stream
(a file or input from a pipeline).
While in some ways similar to an editor which
permits scripted edits (such as <I>ed</I>),
<I>sed</I> works by making only one pass over the
input(s), and is consequently more efficient.
But it is <I>sed</I>'s ability to filter text in a pipeline
which particularly distinguishes it from other types of
editors.
<DL COMPACT>
<DT id="1">
<B>-n</B>, <B>--quiet</B>, <B>--silent</B>
<DT id="2"><DD>
<DD>suppress automatic printing of pattern space
<DT id="3">
<B>--debug</B>
<DT id="4"><DD>
<DD>annotate program execution
<DT id="5">
<B>-e</B> script, <B>--expression</B>=<I>script</I>
<DT id="6"><DD>
<DD>add the script to the commands to be executed
<DT id="7">
<B>-f</B> script-file, <B>--file</B>=<I>script-file</I>
<DT id="8"><DD>
<DD>add the contents of script-file to the commands to be executed
<DT id="9">
<B>--follow-symlinks</B>
<DT id="10"><DD>
<DD>follow symlinks when processing in place
<DT id="11">
<B>-i[SUFFIX]</B>, <B>--in-place</B>[=<I>SUFFIX</I>]
<DT id="12"><DD>
<DD>edit files in place (makes backup if SUFFIX supplied)
<DT id="13">
<B>-l</B> N, <B>--line-length</B>=<I>N</I>
<DT id="14"><DD>
<DD>specify the desired line-wrap length for the `l' command
<DT id="15">
<B>--posix</B>
<DT id="16"><DD>
<DD>disable all GNU extensions.
<DT id="17">
<B>-E</B>, <B>-r</B>, <B>--regexp-extended</B>
<DT id="18"><DD>
<DD>use extended regular expressions in the script
(for portability use POSIX <B>-E</B>).
<DT id="19">
<B>-s</B>, <B>--separate</B>
<DT id="20"><DD>
<DD>consider files as separate rather than as a single,
continuous long stream.
<DT id="21">
<B>--sandbox</B>
<DT id="22"><DD>
<DD>operate in sandbox mode (disable e/r/w commands).
<DT id="23">
<B>-u</B>, <B>--unbuffered</B>
<DT id="24"><DD>
<DD>load minimal amounts of data from the input files and flush
the output buffers more often
<DT id="25">
<B>-z</B>, <B>--null-data</B>
<DT id="26"><DD>
<DD>separate lines by NUL characters
<DT id="27"><B>--help</B><DD>
display this help and exit
<DT id="28"><B>--version</B><DD>
output version information and exit
</DL>
<P>
If no <B>-e</B>, <B>--expression</B>, <B>-f</B>, or <B>--file</B> option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
<P>
GNU sed home page: &lt;<A HREF="https://www.gnu.org/software/sed/">https://www.gnu.org/software/sed/</A>&gt;.
General help using GNU software: &lt;<A HREF="https://www.gnu.org/gethelp/">https://www.gnu.org/gethelp/</A>&gt;.
E-mail bug reports to: &lt;<A HREF="mailto:bug-sed@gnu.org">bug-sed@gnu.org</A>&gt;.
<A NAME="lbAE">&nbsp;</A>
<H2>COMMAND SYNOPSIS</H2>
This is just a brief synopsis of <I>sed</I> commands to serve as
a reminder to those who already know <I>sed</I>;
other documentation (such as the texinfo document)
must be consulted for fuller descriptions.
<A NAME="lbAF">&nbsp;</A>
<H3>Zero-address ``commands''</H3>
<DL COMPACT>
<DT id="29">:&nbsp;<I>label</I>
<DD>
Label for
<B>b</B>
and
<B>t</B>
commands.
<DT id="30">#<I>comment</I>
<DD>
The comment extends until the next newline (or the end of a
<B>-e</B>
script fragment).
<DT id="31">}<DD>
The closing bracket of a { } block.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H3>Zero- or One- address commands</H3>
<DL COMPACT>
<DT id="32">=<DD>
Print the current line number.
<DT id="33">a \<DD>
<DT id="34"><I>text</I>
<DD>
Append
<I>text</I>,
which has each embedded newline preceded by a backslash.
<DT id="35">i \<DD>
<DT id="36"><I>text</I>
<DD>
Insert
<I>text</I>,
which has each embedded newline preceded by a backslash.
<DT id="37">q [<I>exit-code</I>]<DD>
Immediately quit the <I>sed</I> script without processing
any more input, except that if auto-print is not disabled
the current pattern space will be printed. The exit code
argument is a GNU extension.
<DT id="38">Q [<I>exit-code</I>]<DD>
Immediately quit the <I>sed</I> script without processing
any more input. This is a GNU extension.
<DT id="39">r&nbsp;<I>filename</I>
<DD>
Append text read from
<I>filename</I>.
<DT id="40">R&nbsp;<I>filename</I>
<DD>
Append a line read from
<I>filename</I>.
Each invocation of the command reads a line from the file.
This is a GNU extension.
</DL>
<A NAME="lbAH">&nbsp;</A>
<H3>Commands which accept address ranges</H3>
<DL COMPACT>
<DT id="41">{<DD>
Begin a block of commands (end with a }).
<DT id="42">b&nbsp;<I>label</I>
<DD>
Branch to
<I>label</I>;
if
<I>label</I>
is omitted, branch to end of script.
<DT id="43">c \<DD>
<DT id="44"><I>text</I>
<DD>
Replace the selected lines with
<I>text</I>,
which has each embedded newline preceded by a backslash.
<DT id="45">d<DD>
Delete pattern space.
Start next cycle.
<DT id="46">D<DD>
If pattern space contains no newline, start a normal new cycle as if
the d command was issued. Otherwise, delete text in the pattern
space up to the first newline, and restart cycle with the resultant
pattern space, without reading a new line of input.
<DT id="47">h H<DD>
Copy/append pattern space to hold space.
<DT id="48">g G<DD>
Copy/append hold space to pattern space.
<DT id="49">l<DD>
List out the current line in a ``visually unambiguous'' form.
<DT id="50">l&nbsp;<I>width</I>
<DD>
List out the current line in a ``visually unambiguous'' form,
breaking it at
<I>width</I>
characters. This is a GNU extension.
<DT id="51">n N<DD>
Read/append the next line of input into the pattern space.
<DT id="52">p<DD>
Print the current pattern space.
<DT id="53">P<DD>
Print up to the first embedded newline of the current pattern space.
<DT id="54">s/<I>regexp</I>/<I>replacement</I>/
<DD>
Attempt to match
<I>regexp</I>
against the pattern space.
If successful, replace that portion matched
with
<I>replacement</I>.
The
<I>replacement</I>
may contain the special character
<B>&amp;</B>
to refer to that portion of the pattern space which matched,
and the special escapes \1 through \9 to refer to the
corresponding matching sub-expressions in the
<I>regexp</I>.
<DT id="55">t&nbsp;<I>label</I>
<DD>
If a s/// has done a successful substitution since the
last input line was read and since the last t or T
command, then branch to
<I>label</I>;
if
<I>label</I>
is omitted, branch to end of script.
<DT id="56">T&nbsp;<I>label</I>
<DD>
If no s/// has done a successful substitution since the
last input line was read and since the last t or T
command, then branch to
<I>label</I>;
if
<I>label</I>
is omitted, branch to end of script. This is a GNU
extension.
<DT id="57">w&nbsp;<I>filename</I>
<DD>
Write the current pattern space to
<I>filename</I>.
<DT id="58">W&nbsp;<I>filename</I>
<DD>
Write the first line of the current pattern space to
<I>filename</I>.
This is a GNU extension.
<DT id="59">x<DD>
Exchange the contents of the hold and pattern spaces.
<DT id="60">y/<I>source</I>/<I>dest</I>/
<DD>
Transliterate the characters in the pattern space which appear in
<I>source</I>
to the corresponding character in
<I>dest</I>.
</DL>
<A NAME="lbAI">&nbsp;</A>
<H2>Addresses</H2>
<I>Sed</I> commands can be given with no addresses, in which
case the command will be executed for all input lines;
with one address, in which case the command will only be executed
for input lines which match that address; or with two
addresses, in which case the command will be executed
for all input lines which match the inclusive range of
lines starting from the first address and continuing to
the second address.
Three things to note about address ranges:
the syntax is
<I>addr1</I>,<I>addr2</I>
(i.e., the addresses are separated by a comma);
the line which
<I>addr1</I>
matched will always be accepted,
even if
<I>addr2</I>
selects an earlier line;
and if
<I>addr2</I>
is a
<I>regexp</I>,
it will not be tested against the line that
<I>addr1</I>
matched.
<P>
After the address (or address-range),
and before the command, a
<B>!</B>
may be inserted,
which specifies that the command shall only be
executed if the address (or address-range) does
<B>not</B>
match.
<P>
The following address types are supported:
<DL COMPACT>
<DT id="61"><I>number</I>
<DD>
Match only the specified line
<I>number</I>
(which increments cumulatively across files, unless the
<B>-s</B>
option is specified on the command line).
<DT id="62"><I>first</I>~<I>step</I>
<DD>
Match every
<I>step</I>'th
line starting with line
<I>first</I>.
For example, ``sed -n 1~2p'' will print all the odd-numbered lines in
the input stream, and the address 2~5 will match every fifth line,
starting with the second.
<I>first</I>
can be zero; in this case, <I>sed</I> operates as if it were equal to
<I>step</I>.
(This is an extension.)
<DT id="63">$<DD>
Match the last line.
<DT id="64">/<I>regexp</I>/
<DD>
Match lines matching the regular expression
<I>regexp</I>.
Matching is performed on the current pattern space, which
can be modified with commands such as ``s///''.
<DT id="65"><B></B>\c<I>regexp</I><B>c</B>
<DD>
Match lines matching the regular expression
<I>regexp</I>.
The
<B>c</B>
may be any character.
</DL>
<P>
GNU <I>sed</I> also supports some special 2-address forms:
<DL COMPACT>
<DT id="66">0,<I>addr2</I>
<DD>
Start out in &quot;matched first address&quot; state, until
<I>addr2</I>
is found.
This is similar to
1,<I>addr2</I>,
except that if
<I>addr2</I>
matches the very first line of input the
0,<I>addr2</I>
form will be at the end of its range, whereas the
1,<I>addr2</I>
form will still be at the beginning of its range.
This works only when
<I>addr2</I>
is a regular expression.
<DT id="67"><I>addr1</I>,+<I>N</I>
<DD>
Will match
<I>addr1</I>
and the
<I>N</I>
lines following
<I>addr1</I>.
<DT id="68"><I>addr1</I>,~<I>N</I>
<DD>
Will match
<I>addr1</I>
and the lines following
<I>addr1</I>
until the next line whose input line number is a multiple of
<I>N</I>.
</DL>
<A NAME="lbAJ">&nbsp;</A>
<H2>REGULAR EXPRESSIONS</H2>
POSIX.2 BREs
<I>should</I>
be supported, but they aren't completely because of performance
problems.
The
<B>\n</B>
sequence in a regular expression matches the newline character,
and similarly for
<B>\a</B>,
<B>\t</B>,
and other sequences.
The <I>-E</I> option switches to using extended regular expressions instead;
it has been supported for years by GNU sed, and is now
included in POSIX.
<A NAME="lbAK">&nbsp;</A>
<H2>BUGS</H2>
<P>
E-mail bug reports to
<B><A HREF="mailto:bug-sed@gnu.org">bug-sed@gnu.org</A></B>.
Also, please include the output of ``sed --version'' in the body
of your report if at all possible.
<A NAME="lbAL">&nbsp;</A>
<H2>AUTHOR</H2>
Written by Jay Fenlason, Tom Lord, Ken Pizzini,
Paolo Bonzini, Jim Meyering, and Assaf Gordon.
GNU sed home page: &lt;<A HREF="https://www.gnu.org/software/sed/">https://www.gnu.org/software/sed/</A>&gt;.
General help using GNU software: &lt;<A HREF="https://www.gnu.org/gethelp/">https://www.gnu.org/gethelp/</A>&gt;.
E-mail bug reports to: &lt;<A HREF="mailto:bug-sed@gnu.org">bug-sed@gnu.org</A>&gt;.
<A NAME="lbAM">&nbsp;</A>
<H2>COPYRIGHT</H2>
Copyright &#169; 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later &lt;<A HREF="https://gnu.org/licenses/gpl.html">https://gnu.org/licenses/gpl.html</A>&gt;.
<BR>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
<A NAME="lbAN">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+awk">awk</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+ed">ed</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+grep">grep</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+tr">tr</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+perlre">perlre</A></B>(1),
sed.info,
any of various books on <I>sed</I>,
the <I>sed</I> FAQ (<A HREF="http://sed.sf.net/grabbag/tutorials/sedfaq.txt),">http://sed.sf.net/grabbag/tutorials/sedfaq.txt),</A>
<A HREF="http://sed.sf.net/grabbag/.">http://sed.sf.net/grabbag/.</A>
<P>
The full documentation for
<B>sed</B>
is maintained as a Texinfo manual. If the
<B>info</B>
and
<B>sed</B>
programs are properly installed at your site, the command
<DL COMPACT>
<DT id="69"><DD>
<B>info sed</B>
</DL>
<P>
should give you access to the complete manual.
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="70"><A HREF="#lbAB">NAME</A><DD>
<DT id="71"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="72"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="73"><A HREF="#lbAE">COMMAND SYNOPSIS</A><DD>
<DL>
<DT id="74"><A HREF="#lbAF">Zero-address ``commands''</A><DD>
<DT id="75"><A HREF="#lbAG">Zero- or One- address commands</A><DD>
<DT id="76"><A HREF="#lbAH">Commands which accept address ranges</A><DD>
</DL>
<DT id="77"><A HREF="#lbAI">Addresses</A><DD>
<DT id="78"><A HREF="#lbAJ">REGULAR EXPRESSIONS</A><DD>
<DT id="79"><A HREF="#lbAK">BUGS</A><DD>
<DT id="80"><A HREF="#lbAL">AUTHOR</A><DD>
<DT id="81"><A HREF="#lbAM">COPYRIGHT</A><DD>
<DT id="82"><A HREF="#lbAN">SEE ALSO</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:26 GMT, March 31, 2021
</BODY>
</HTML>