314 lines
15 KiB
HTML
314 lines
15 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
|
<!-- saved from url=(0055)http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?sed+C -->
|
|
<HTML><HEAD><TITLE>sed(C)</TITLE>
|
|
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
|
|
<META content="MSHTML 5.00.2614.3500" name=GENERATOR></HEAD>
|
|
<BODY><A
|
|
href="http://osr5doc.ca.caldera.com:457/cgi-bin/getnav/man/html.C/sed.C.html">
|
|
<H1>sed(C)</H1></A>
|
|
<HR>
|
|
<B>sed -- <!--Meta NM "sed"-->invoke the stream editor </B><!--Meta DN "invoke the stream editor"-->
|
|
<P>
|
|
<H2>Syntax</H2><B>sed</B> [ <B>-n</B> ] [ <I>script</I> ] [ <B>-f</B>
|
|
<I>sfile</I> ] [ <I>file</I> ... ]
|
|
<P><B>sed</B> [ <B>-n</B> ] [ <B>-e</B> <I>script</I> ] ... [ <B>-f</B>
|
|
<I>sfile</I> ] ... [ <I>file</I> ... ]
|
|
<H2>Description</H2>The <B>sed</B> command copies the named <I>files</I>
|
|
(standard input default) to the standard output, edited according to a script of
|
|
commands.
|
|
<P><B>sed</B> takes the following options:
|
|
<DL compact>
|
|
<P>
|
|
<DT><B>-e</B> <I>script</I>
|
|
<DD>Read the command <I>script</I>; usually quoted to protect it from the
|
|
shell.
|
|
<P></P>
|
|
<DT><B>-f</B> <I>sfile</I>
|
|
<DD>Take the script from the file <I>sfile</I>; these options accumulate. If
|
|
there is just one <B>-e</B> option and no <B>-f</B> options, the flag
|
|
<B>-e</B> may be omitted.
|
|
<P></P>
|
|
<DT><B>-n</B>
|
|
<DD>Suppress the default output. </DD></DL>A <I>script</I> consists of editing
|
|
commands, one per line, of the following form: <BR><BR>[ <I>address</I> [ ,
|
|
<I>address</I> ] ] <I>function</I> [ <I>arguments</I> ]
|
|
<P>In normal operation, <B>sed</B> cyclically copies a line of input into a
|
|
pattern space (unless there is something left after a <B>D</B> command), applies
|
|
in sequence all commands whose addresses select that pattern space, and at the
|
|
end of the script copies the pattern space to the standard output (except under
|
|
<B>-n</B>) and deletes the pattern space.
|
|
<P>A semicolon ``;'' can be used as a command delimiter.
|
|
<P>Some of the commands use a hold space to save all or part of the pattern
|
|
space for subsequent retrieval (see the ``Limitations'' section).
|
|
<P>An address is either a decimal number that counts input lines cumulatively
|
|
across files, a ``$'' that addresses the last line of input, or a context
|
|
address, that is, a <I>/regular expression/</I> as described in <A
|
|
href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?regexp+M"><B>regexp</B>(M)</A>,
|
|
modified as follows:
|
|
<UL>
|
|
<P>
|
|
<LI>In a context address, the construction <I>\?regular expression?</I>, where
|
|
``?'' is any character, is identical to <I>/regular expression/</I>. Note that
|
|
in the context address <I>\xabc\xdefx</I>, the second <I>x</I> stands for
|
|
itself, so that the standard expression is <I>abcxdef</I>.
|
|
<P></P>
|
|
<LI>The escape sequence \n matches a newline embedded in the pattern space.
|
|
<P></P>
|
|
<LI>A dot (.) matches any character except the terminal newline of the pattern
|
|
space.
|
|
<P></P>
|
|
<LI>A command line with no addresses selects every pattern space.
|
|
<P></P>
|
|
<LI>A command line with one address selects each pattern space that matches
|
|
the address.
|
|
<P></P>
|
|
<LI>A command line with two addresses separated by a comma selects the
|
|
inclusive range from the first pattern space that matches the first address
|
|
through the next pattern space that matches the second. (If the second address
|
|
is a number less than or equal to the line number first selected, only one
|
|
line is selected.) Thereafter, the process is repeated, looking again for the
|
|
first address. </LI></UL>Editing commands can be applied only to nonselected
|
|
pattern spaces by use of the negation function ``!'' described in the next
|
|
section.
|
|
<H3>Functions</H3>In the following list of functions, the maximum number of
|
|
permissible addresses for each function is indicated in parentheses.
|
|
<P>The <I>text</I> argument consists of one or more lines, all but the last of
|
|
which end with backslashes to hide the newlines. Backslashes in text are treated
|
|
like backslashes in the replacement string of an <B>s</B> command, and may be
|
|
used to protect initial blanks and tabs against the stripping that is done on
|
|
every script line.
|
|
<P>The <I>rfile</I> or <I>wfile</I> argument must terminate the command line and
|
|
must be preceded by one blank. Each <I>wfile</I> is created before processing
|
|
begins. There can be at most 10 distinct <I>wfile</I> arguments.
|
|
<DL compact>
|
|
<P>
|
|
<DT>(1) <B>a\</B><BR><I>text</I>
|
|
<DD>Appends <I>text</I>, placing it on the output before reading the next
|
|
input line. Note that there must be a line break between the command and the
|
|
<I>text</I>.
|
|
<P></P>
|
|
<DT>(2) <B>b</B> <I>label</I>
|
|
<DD>Branches to the <B>:</B> command bearing the <I>label</I>. If <I>label</I>
|
|
is empty, branches to the end of the script.
|
|
<P></P>
|
|
<DT>(2) <B>c\</B><BR><I>text</I>
|
|
<DD>Changes text by deleting the pattern space and then appending <I>text</I>.
|
|
With 0 or 1 address or at the end of a 2-address range, places <I>text</I> on
|
|
the output and starts the next cycle. Note that there must be a line break
|
|
between the command and the <I>text</I>.
|
|
<P></P>
|
|
<DT>(2) <B>d</B>
|
|
<DD>Deletes the pattern space and starts the next cycle.
|
|
<P></P>
|
|
<DT>(2) <B>D</B>
|
|
<DD>Deletes the initial segment of the pattern space through the first newline
|
|
and starts the next cycle.
|
|
<P></P>
|
|
<DT>(2) <B>g</B>
|
|
<DD>Replaces the contents of the pattern space with the contents of the hold
|
|
space.
|
|
<P></P>
|
|
<DT>(2) <B>G</B>
|
|
<DD>Appends the contents of the hold space to the pattern space.
|
|
<P></P>
|
|
<DT>(2) <B>h</B>
|
|
<DD>Replaces the contents of the hold space with the contents of the pattern
|
|
space.
|
|
<P></P>
|
|
<DT>(2) <B>H</B>
|
|
<DD>Appends the contents of the pattern space to the hold space.
|
|
<P></P>
|
|
<DT>(1) <B>i\</B><BR><I>text</I>
|
|
<DD>Insert. Places <I>text</I> on the standard output. Note that there must be
|
|
a line break between the command and the <I>text</I>.
|
|
<P></P>
|
|
<DT>(2) <B>l</B>
|
|
<DD>Lists the pattern space on the standard output in an unambiguous way.
|
|
Nonprinting characters are displayed as a three digit octal number preceded by
|
|
a backslash ``\''. The following characters are printed as escape sequences: <PRE> -------------------------------------------
|
|
Character Output
|
|
-------------------------------------------
|
|
backslash \\
|
|
alert (bell) \a
|
|
backspace \b
|
|
form feed \f
|
|
carriage return \r
|
|
horizontal tab \t
|
|
vertical tab \v
|
|
</PRE>Any output lines that are longer than the output device width
|
|
(determined by the environment variable COLUMNS) are folded into multiple
|
|
lines. New lines, inserted when folding a long line, are escaped by a
|
|
preceding backslash character. The ends of each line in the pattern space are
|
|
denoted by a dollar character ``$''.
|
|
<P></P>
|
|
<DT>(2) <B>n</B>
|
|
<DD>Copies the pattern space to the standard output. Replaces the pattern
|
|
space with the next line of input.
|
|
<P></P>
|
|
<DT>(2) <B>N</B>
|
|
<DD>Appends the next line of input to the pattern space with an embedded
|
|
newline. (The current line number changes.)
|
|
<P></P>
|
|
<DT>(2) <B>p</B>
|
|
<DD>Prints (copies) the pattern space on the standard output.
|
|
<P></P>
|
|
<DT>(2) <B>P</B>
|
|
<DD>Prints (copies) the initial segment of the pattern space through the first
|
|
newline to the standard output.
|
|
<P></P>
|
|
<DT>(1) <B>q</B>
|
|
<DD>Quits <B>sed</B> by branching to the end of the script. No new cycle is
|
|
started.
|
|
<P></P>
|
|
<DT>(2) <B>r</B> <I>rfile</I>
|
|
<DD>Reads the contents of <I>rfile</I> and places them on the output before
|
|
reading the next input line.
|
|
<P></P>
|
|
<DT>(2) <B>s</B> <I>/regular expression/replacement/flags</I>
|
|
<DD>Substitutes the <I>replacement</I> string for instances of the <I>regular
|
|
expression</I> in the pattern space. Any character may be used instead of
|
|
``/''. For a more detailed description, see <A
|
|
href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?regexp+M"><B>regexp</B>(M)</A>.
|
|
|
|
<P><I>Flags</I> is zero or more of:
|
|
<DL compact>
|
|
<P>
|
|
<DT><I>n</I>
|
|
<DD>Substitute for just the <I>n</I>th occurrence of the <I>regular
|
|
expression</I>. <I>n</I> must be an integer greater than zero.
|
|
<P></P>
|
|
<DT><B>g</B>
|
|
<DD>Globally substitutes for all non-overlapping instances of the <I>regular
|
|
expression</I> rather than just the first one.
|
|
<P></P>
|
|
<DT><B>p</B>
|
|
<DD>Prints the pattern space if a replacement was made.
|
|
<P></P>
|
|
<DT><B>w</B> <I>wfile</I>
|
|
<DD>Writes the pattern space to <I>wfile</I> if a replacement was made.
|
|
</DD></DL>
|
|
<P></P>
|
|
<DT>(2) <B>t</B> <I>label</I>
|
|
<DD>Branches to the colon (:) command bearing <I>label</I> if any
|
|
substitutions have been made since the most recent reading of an input line or
|
|
execution of a <B>t</B> command. If <I>label</I> is empty, <B>t</B> branches
|
|
to the end of the script.
|
|
<P></P>
|
|
<DT>(2) <B>w</B> <I>wfile</I>
|
|
<DD>Writes the pattern space to <I>wfile</I>.
|
|
<P></P>
|
|
<DT>(2) <B>x</B>
|
|
<DD>Exchanges the contents of the pattern and hold spaces.
|
|
<P></P>
|
|
<DT>(2) <B>y</B> <I>/string1/string2/</I>
|
|
<DD>Replaces all occurrences of characters in <I>string1</I> with the
|
|
corresponding characters in <I>string2</I>. The lengths of <I>string1</I> and
|
|
<I>string2</I> must be equal.
|
|
<P></P>
|
|
<DT>(2) <B>!</B> <I>function</I>
|
|
<DD>Applies the <I>function</I> (or group, if <I>function</I> is ``{'') only
|
|
to lines <I>not</I> selected by the address(es).
|
|
<P></P>
|
|
<DT>(0) <B>:</B> <I>label</I>
|
|
<DD>This command does nothing; it bears a <I>label</I> for <B>b</B> and
|
|
<B>t</B> commands to branch to. Labels can be at most 8 characters long.
|
|
<P></P>
|
|
<DT>(1) <B>=</B>
|
|
<DD>Places the current line number on the standard output as a line.
|
|
<P></P>
|
|
<DT>(2) <B>{</B>
|
|
<DD>Executes the following commands through a matching ``}'' only when the
|
|
pattern space is selected.
|
|
<P></P>
|
|
<DT>(2) <B>!{</B>
|
|
<DD>Executes the following commands through a matching ``}'' only when the
|
|
pattern space is not selected.
|
|
<P></P>
|
|
<DT>(0)
|
|
<DD>An empty command is ignored.
|
|
<P></P>
|
|
<DT>(0) <B>#</B>
|
|
<DD>Ignore the remainder of the line if <B>#</B> is followed by any other
|
|
character than ``n'' (treat the line as a comment); if the character ``n''
|
|
follows <B>#</B>, suppress the default output (equivalent to the command line
|
|
option <B>-n</B>). </DD></DL>
|
|
<H3>Environment variables</H3>
|
|
<DL compact>
|
|
<P>
|
|
<DT>COLUMNS
|
|
<DD>The width of the standard output device in characters; used by the
|
|
<B>l</B> command for folding long lines. If this variable is not set or it has
|
|
an invalid value, <B>sed</B> uses the default value 72. </DD></DL>
|
|
<H2>Exit values</H2><B>sed</B> continues to process all <I>file</I> arguments
|
|
even if one or more of them produces an open error. If there is an open error,
|
|
<B>sed</B> will exit with a value of 1 when it has finished processing the
|
|
files. A value of 2 indicates a usage error.
|
|
<H2>Examples</H2>The following examples assume the use of <A
|
|
href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?sh+C"><B>sh</B>(C)</A>
|
|
or <A
|
|
href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?ksh+C"><B>ksh</B>(C)</A>.
|
|
|
|
<P>A common use of <B>sed</B> is to edit a file from within a shell script. In
|
|
this example, every occurrence of the string ``sysman'' in the file
|
|
<I>infile</I> is replaced by ``System Manager''. A temporary file TMP is used to
|
|
hold the intermediate result of the edit: <PRE> TMP=/usr/tmp/tmpfile_$$
|
|
sed -e 's/sysman/System Manager/g' < infile > $TMP
|
|
mv $TMP infile
|
|
</PRE>In this example, <B>sed</B> removes all blank lines (including those with
|
|
just <Tab> and <Space> characters) from <I>padded_file</I>: <PRE> sed '
|
|
/^$/ d
|
|
/^[<Tab><Space>]*$/ d
|
|
´ padded_file
|
|
</PRE><B>sed</B> can be used to strip all lines from a file which do not contain
|
|
a certain string. In this example, all lines in the file <I>infile</I> which
|
|
start with a hash ``#'' are echoed to the screen: <BR><BR><B>sed -e '/^#/!d'
|
|
< infile</B>
|
|
<P>If several editing commands must be carried out on a file, but the parameters
|
|
for the edit are to be supplied by the user, then use <B>echo</B> to append
|
|
command lines to a <B>sed</B> script. The following example removes all
|
|
occurrences of the strings given as arguments to the script from the file
|
|
<I>infile</I>. The name of the temporary file is held by the variable SCRIPT: <PRE> SCRIPT=/usr/tmp/script_$$
|
|
<P>
|
|
for name in $*
|
|
do
|
|
echo "s/${name}//g" >> $SCRIPT
|
|
done
|
|
<P>
|
|
TMPFILE=/usr/tmp/tmpfile_$$
|
|
<P>
|
|
sed -f $SCRIPT < infile > $TMPFILE
|
|
<P>
|
|
mv $TMPFILE infile
|
|
<P>
|
|
rm $SCRIPT
|
|
</P></PRE>Another use of <B>sed</B> is to process the output from other
|
|
commands. Here the <B>ps</B> command is filtered using <B>sed</B> to report the
|
|
status of all processes other than those owned by the super user: <BR><BR><B>ps
|
|
-ef | sed -e '/^[<Space><Tab>]*root/d'</B>
|
|
<P>
|
|
<H2>Limitations</H2>Both the hold space and pattern space can hold a maximum of
|
|
8192 bytes. <BR>
|
|
<H2>See also</H2><A
|
|
href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?awk+C"><B>awk</B>(C)</A>,
|
|
<A
|
|
href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?ed+C"><B>ed</B>(C)</A>,
|
|
<A
|
|
href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?grep+C"><B>grep</B>(C)</A>,
|
|
<A
|
|
href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?regexp+M"><B>regexp</B>(M)</A>
|
|
|
|
<P><A
|
|
href="http://osr5doc.ca.caldera.com:457/OSUserG/_Manipulating_text_with_sed.html">Chapter
|
|
14, ``Manipulating text with sed''</A> in the <I>Operating System User's
|
|
Guide</I>
|
|
<H2>Standards conformance</H2><B>sed</B> is conformant with:
|
|
<P>ISO/IEC DIS 9945-2:1992, Information technology - Portable Operating System
|
|
Interface (POSIX) - Part 2: Shell and Utilities (IEEE Std 1003.2-1992);
|
|
<BR>AT&T SVID Issue 2; <BR>X/Open CAE Specification, Commands and Utilities,
|
|
Issue 4, 1992.
|
|
<P><I>SCO OpenServer Release 5.0.6 -- 1 August 2000</I> <LINK
|
|
href="/man/html.C/CONTENTS.html" rel=contents><LINK
|
|
href="/cgi-bin/getnav/man/html.C/sed.C.html" rel=navigate><LINK
|
|
href="/cgi-bin/man/man?setcolor+C" rel=next><LINK
|
|
href="/cgi-bin/man/man?sdiff+C" rel=previous><!--NAVIGATE <TITLEgReATerTHanNavigational information for sed(C)</TITLEgReATerTHan --><!--NAVIGATE <H2gReATerTHanNavigational information for sed(C)</H2gReATerTHan --><!--NAVIGATE <ULgReATerTHan --><!--NAVIGATE <LIgReATerTHan --><!--NAVIGATE Book title: man(C) --><!--NAVIGATE <LIgReATerTHan --><!--NAVIGATE Next topic: --><!--NAVIGATE <A HREF=/cgi-bin/man/man?setcolor+CgReATerTHansetcolor(C)</AgReATerTHan --><!--NAVIGATE <LIgReATerTHan --><!--NAVIGATE Previous topic: --><!--NAVIGATE <A HREF=/cgi-bin/man/man?sdiff+CgReATerTHansdiff(C)</AgReATerTHan --><!--NAVIGATE <LIgReATerTHan --><!--NAVIGATE <A HREF=/man/html.C/CONTENTS.htmlgReATerTHanTable of contents</AgReATerTHan --><!--NAVIGATE </ULgReATerTHan --><!--NAVIGATE <BRgReATerTHan --><!--NAVIGATE <BRgReATerTHan --><!--NAVIGATE <A HREF=/man/html.C/COPYRIGHT.htmlgReATerTHan© 2000 The Santa Cruz Operation, Inc. All rights reserved.</AgReATerTHan --><!--NAVIGATE <LINK HREF=/man/html.C/CONTENTS.html REL=contentsgReATerTHan --><!--NAVIGATE <LINK HREF=/cgi-bin/man/man?setcolor+C REL=nextgReATerTHan --><!--NAVIGATE <LINK HREF=/cgi-bin/man/man?sdiff+C REL=previousgReATerTHan --><!--NAVIGATE --></P></BODY></HTML>
|