481 lines
16 KiB
HTML
481 lines
16 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content=
|
|
"text/html; charset=utf-8">
|
|
<title>sed - Stream editor</title>
|
|
<LINK rel="stylesheet" type="text/css" href="../rzahg/ic.css">
|
|
</HEAD>
|
|
<body bgcolor="#FFFFFF">
|
|
<!-- Java sync-link -->
|
|
<SCRIPT LANGUAGE="Javascript" SRC="../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
|
|
|
|
|
|
|
|
<h2>sed - Stream editor</h2>
|
|
|
|
<p><strong>Synopsis</strong></p>
|
|
|
|
<p><img src="delta.gif" alt="Start of change">
|
|
<strong>sed [-an] [-C</strong> <em>ccsid</em> <strong>]</strong>
|
|
<em>command file</em> ...</p>
|
|
|
|
<p><strong>sed [-an] [-C</strong> <em>ccsid</em> <strong>] [-e</strong>
|
|
<em>command</em><strong>] [-f</strong> <em>command_file</em><strong>]</strong>
|
|
<em>file ...</em>
|
|
<img src="deltaend.gif" alt="End of change"></p>
|
|
|
|
<p><strong>Description</strong></p>
|
|
|
|
<p>The <strong>sed</strong> utility reads the specified <em>
|
|
files</em>, or the standard input if no files are specified,
|
|
modifying the input as specified by a list of <em>commands</em>.
|
|
The input is then written to the standard output.</p>
|
|
|
|
<p>A single <em>command</em> may be specified as the first argument
|
|
to <strong>sed</strong>. Multiple commands may be specified by
|
|
using the <strong>-e</strong> or <strong>-f</strong> options. All
|
|
commands are applied to the input in the order they are specified
|
|
regardless of their origin.</p>
|
|
|
|
<p><strong>Options</strong></p>
|
|
|
|
<dl>
|
|
<dt><strong>-a</strong></dt>
|
|
|
|
<dd>By default, the files listed as parameters for the <strong>
|
|
w</strong> functions are created (or truncated) before any
|
|
processing begins. The <strong>-a</strong> option causes <strong>
|
|
sed</strong> to delay opening each file until a command containing
|
|
the related <strong>w</strong> function is applied to a line of
|
|
input.</dd>
|
|
|
|
<dt><img src="delta.gif" alt="Start of change"><strong>-C</strong> <em>ccsid</em></dt>
|
|
|
|
<dd>Any files created by <strong>sed</strong> are created with the CCSID specified by
|
|
<em>ccsid</em>. This option overrides the value of the QIBM_CCSID environment
|
|
variable.<img src="deltaend.gif" alt="End of change"></dd>
|
|
|
|
<dt><strong>-e</strong> <em>command</em></dt>
|
|
|
|
<dd>Append the editing commands specified by the <em>command</em>
|
|
argument to the list of commands.</dd>
|
|
|
|
<dt><strong>-f</strong> <em>command_file</em></dt>
|
|
|
|
<dd>Append the editing commands found in the file <em>
|
|
command_file</em> to the list of commands. The editing commands
|
|
should each be listed on a separate line.</dd>
|
|
|
|
<dt><strong>-n</strong></dt>
|
|
|
|
<dd>By default, each line of input is echoed to the standard output
|
|
after all of the commands have been applied to it. The <strong>
|
|
-n</strong> option suppresses this behavior.</dd>
|
|
</dl>
|
|
|
|
<p><strong>Operands</strong></p>
|
|
|
|
<p>The form of a <strong>sed</strong> command is as follows:</p>
|
|
|
|
<pre>
|
|
[<em>address</em>[,<em>address</em>]]<em>function</em>[<em>arguments</em>]
|
|
</pre>
|
|
|
|
White space may be inserted before the first <em>address</em> and
|
|
the <em>function</em> portions of the command.
|
|
|
|
<p>Normally, <strong>sed</strong> cyclically copies a line of
|
|
input, not including its terminating newline character, into a
|
|
"pattern space", (unless there is something left after a <strong>
|
|
D</strong> function), applies all of the commands with addresses
|
|
that select that pattern space, copies the pattern space to the
|
|
standard output, appending a newline, and deletes the pattern
|
|
space.</p>
|
|
|
|
<p>Some of the functions use a "hold space" to save all or part of
|
|
the pattern space for subsequent retrieval.</p>
|
|
|
|
<p><strong>Extended Description</strong></p>
|
|
|
|
<p><strong><em>sed Addresses</em></strong></p>
|
|
|
|
<p>An address is not required, but if specified must be:</p>
|
|
|
|
<ul>
|
|
<li>a number that counts input lines cumulatively across input
|
|
files,</li>
|
|
|
|
<li>a dollar ($) character that addresses the last line of input,
|
|
or</li>
|
|
|
|
<li>a context address which consists of a regular expression
|
|
preceded and followed by a delimiter.</li>
|
|
</ul>
|
|
|
|
<p>A command line with no addresses selects every pattern
|
|
space.</p>
|
|
|
|
<p>A command line with one address selects all of the pattern
|
|
spaces that match the address.</p>
|
|
|
|
<p>A command line with two addresses 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 that line is selected. Starting at the first line
|
|
following the selected range, <strong>sed</strong> starts looking
|
|
again for the first address.</p>
|
|
|
|
<p>Editing commands can be applied to non-selected pattern spaces
|
|
by use of the exclamation character (!) function.</p>
|
|
|
|
<p><strong><em>sed Regular Expressions</em></strong></p>
|
|
|
|
<p><strong>sed</strong> regular expressions are basic regular
|
|
expressions. In addition, <strong>sed</strong> has the following
|
|
two additions to basic regular expressions:</p>
|
|
|
|
<ul>
|
|
<li>In a context address, any character other than a backslash (\)
|
|
or newline character may be used to delimit the regular expression.
|
|
Also, putting a backslash character before the delimiting character
|
|
causes the character to be treated literally. For example, in the
|
|
context address <em>\xabc\xdefx</em>, the regular expression
|
|
delimiter is an <em>x</em> and the second <em>x</em> stands for
|
|
itself, so that the regular expression is <em>abcxdef</em> .</li>
|
|
|
|
<li>The escape sequence \n matches a newline character embedded in
|
|
the pattern space. You can't, however, use a literal newline
|
|
character in an address or in the substitute command.</li>
|
|
</ul>
|
|
|
|
<p>One special feature of <strong>sed</strong> regular expressions
|
|
is that they can default to the last regular expression used. If a
|
|
regular expression is empty, that is, just the delimiter characters are
|
|
specified, the last regular expression encountered is used instead.
|
|
The last regular expression is defined as the last regular
|
|
expression used as part of an address or substitute command, and at
|
|
run-time, not compile-time. For example, the command:</p>
|
|
|
|
<pre>
|
|
/abc/s//XXX/
|
|
</pre>
|
|
|
|
<p>will substitute XXX for the pattern abc.</p>
|
|
|
|
<p><strong><em>sed Functions</em></strong></p>
|
|
|
|
<p>In the following list of commands, the maximum number of
|
|
permissible addresses for each command is indicated by
|
|
[<em>0addr</em>], [<em>1addr</em>], or [<em>2addr</em>],
|
|
representing zero, one, or two addresses.</p>
|
|
|
|
<p>The argument <em>text</em> consists of one or more lines. To
|
|
embed a newline in the text, precede it with a backslash. Other
|
|
backslashes in <em>text</em> are deleted and the following
|
|
character taken literally.</p>
|
|
|
|
<p>The <strong>r</strong> and <strong>w</strong> functions take an
|
|
optional <em>file</em> parameter, which should be separated from
|
|
the function letter by white space. Each file given as an argument
|
|
to <strong>sed</strong> is created (or its contents truncated)
|
|
before any input processing begins.</p>
|
|
|
|
<p>The <strong>b</strong>, <strong>r</strong>,<strong>s</strong>,
|
|
<strong>
|
|
t</strong>,<strong>w</strong>,<strong>y</strong>,<strong>!</strong>
|
|
, and <strong>&</strong> functions all accept additional
|
|
arguments. The following synopses indicate which arguments have to
|
|
be separated from the function letters by white space
|
|
characters.</p>
|
|
|
|
<p>Two of the functions take a <em>function-list</em>. This is a
|
|
list of <strong>sed</strong> functions separated by newlines, as
|
|
follows:</p>
|
|
|
|
<pre>
|
|
{ function
|
|
function
|
|
...
|
|
function
|
|
}
|
|
</pre>
|
|
|
|
<p>The <strong>{</strong> can be preceded by white space and can be
|
|
followed by white space. The function can be preceded by white
|
|
space. The terminating <strong>}</strong> must be preceded by a
|
|
newline or optional white space.</p>
|
|
|
|
<dl>
|
|
<dt><strong>[<em>2addr</em>] <em>function-list</em></strong></dt>
|
|
|
|
<dd>Execute <em>function-list</em> only when the pattern space is
|
|
selected.</dd>
|
|
|
|
<dt><strong>[<em>1addr</em>]a\ <em>text</em></strong></dt>
|
|
|
|
<dd>Write <em>text</em> to standard output immediately before each
|
|
attempt to read a line of input, whether by executing the <strong>
|
|
N</strong> function or by beginning a new cycle.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]b[<em>label</em>]</strong></dt>
|
|
|
|
<dd>Branch to the <strong>&</strong> function with the
|
|
specified <em>label</em>. If the <em>label</em> is not specified,
|
|
branch to the end of the script.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]c\ <em>text</em></strong></dt>
|
|
|
|
<dd>Delete the pattern space. With 0 or 1 address or at the end of
|
|
a 2-address range, <em>text</em> is written to the standard
|
|
output.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]d</strong></dt>
|
|
|
|
<dd>Delete the pattern space and start the next cycle.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]D</strong></dt>
|
|
|
|
<dd>Delete the initial segment of the pattern space through the
|
|
first newline character and start the next cycle.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]g</strong></dt>
|
|
|
|
<dd>Replace the contents of the pattern space with the contents of
|
|
the hold space.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]G</strong></dt>
|
|
|
|
<dd>Append a newline character followed by the contents of the hold
|
|
space to the pattern space.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]h</strong></dt>
|
|
|
|
<dd>Replace the contents of the hold space with the contents of the
|
|
pattern space.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]H</strong></dt>
|
|
|
|
<dd>Append a newline character followed by the contents of the
|
|
pattern space to the hold space.</dd>
|
|
|
|
<dt><strong>[<em>1addr</em>]i\ <em>text</em></strong></dt>
|
|
|
|
<dd>Write <em>text</em> to the standard output.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]l</strong></dt>
|
|
|
|
<dd>(The letter ell.) Write the pattern space to the standard
|
|
output in a visually unambiguous form. This form is as follows:
|
|
|
|
<ul>
|
|
<li>backslash (\)</li>
|
|
|
|
<li>alert (\a)</li>
|
|
|
|
<li>form-feed (\f)</li>
|
|
|
|
<li>newline (\n)</li>
|
|
|
|
<li>carriage-return (\r)</li>
|
|
|
|
<li>tab (\t)</li>
|
|
|
|
<li>vertical tab (\v)</li>
|
|
</ul>
|
|
|
|
<p>Nonprintable characters are written as three-digit octal numbers
|
|
(with a preceding backslash) for each byte in the character (most
|
|
significant byte first). Long lines are folded, with the point of
|
|
folding indicated by displaying a backslash followed by a newline.
|
|
The end of each line is marked with a dollar sign ($).</p>
|
|
</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]n</strong></dt>
|
|
|
|
<dd>Write the pattern space to the standard output if the default
|
|
output has not been suppressed, and replace the pattern space with
|
|
the next line of input.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]N</strong></dt>
|
|
|
|
<dd>Append the next line of input to the pattern space, using an
|
|
embedded newline character to separate the appended material from
|
|
the original contents. Note that the current line number
|
|
changes.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]p</strong></dt>
|
|
|
|
<dd>Write the pattern space to standard output.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]P</strong></dt>
|
|
|
|
<dd>Write the pattern space, up to the first newline character to
|
|
the standard output.</dd>
|
|
|
|
<dt><strong>[<em>1addr</em>]q</strong></dt>
|
|
|
|
<dd>Branch to the end of the script and quit without starting a new
|
|
cycle.</dd>
|
|
|
|
<dt><strong>[<em>1addr</em>]r <em>file</em></strong></dt>
|
|
|
|
<dd>Copy the contents of <em>file</em> to the standard output
|
|
immediately before the next attempt to read a line of input. If
|
|
<em>file</em> cannot be read for any reason, it is silently ignored
|
|
and no error condition is set.</dd>
|
|
|
|
<dt><strong>
|
|
[<em>2addr</em>]s/<em>regular_expression</em>/<em>replacement</em>/<em>
|
|
flags</em></strong></dt>
|
|
|
|
<dd>Substitute the <em>replacement</em> string for the first
|
|
instance of the <em>regular_expression</em> in the pattern space.
|
|
Any character other than backslash or newline can be used instead
|
|
of a slash to delimit the <em>regular_expression</em> and the <em>
|
|
replacement</em>. Within the <em>regular_expression</em> and the
|
|
<em>replacement</em>, the regular expression delimiter itself can
|
|
be used as a literal character if it is preceded by a backslash.
|
|
|
|
<p>An ampersand (&) appearing in the <em>replacement</em> is
|
|
replaced by the string matching the regular expression. The special
|
|
meaning of & in this context can be suppressed by preceding it
|
|
by a backslash. The string \#, where # is a digit, is replaced by
|
|
the text matched by the corresponding backreference expression.</p>
|
|
|
|
<p>A line can be split by substituting a newline character into it.
|
|
To specify a newline character in the replacement string, precede
|
|
it with a backslash.</p>
|
|
|
|
<p>The value of <em>flags</em> in the substitute function is zero
|
|
or more of the following:</p>
|
|
|
|
<dl>
|
|
<dt><strong>0 ... 9</strong></dt>
|
|
|
|
<dd>Make the substitution only for the N'th occurrence of the
|
|
regular expression in the pattern space.</dd>
|
|
|
|
<dt><strong>g</strong></dt>
|
|
|
|
<dd>Make the substitution for all non-overlapping matches of the
|
|
regular expression, not just the first one.</dd>
|
|
|
|
<dt><strong>p</strong></dt>
|
|
|
|
<dd>Write the pattern space to standard output if a replacement was
|
|
made. If the replacement string is identical to that which it
|
|
replaces, it is still considered to have been a replacement.</dd>
|
|
|
|
<dt><strong>w <em>file</em></strong></dt>
|
|
|
|
<dd>Append the pattern space to <em>file</em> if a replacement was
|
|
made. If the replacement string is identical to that which it
|
|
replaces, it is still considered to have been a replacement.</dd>
|
|
</dl>
|
|
</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]t [<em>label</em>]</strong></dt>
|
|
|
|
<dd>Branch to the <strong>:</strong> function bearing the <em>
|
|
label</em> if any substitutions have been made since the most
|
|
recent reading of an input line or execution of a <strong>
|
|
t</strong> function. If no <em>label</em> is specified, branch to
|
|
the end of the script.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]w <em>file</em></strong></dt>
|
|
|
|
<dd>Append the pattern space to the <em>file</em>.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]x</strong></dt>
|
|
|
|
<dd>Swap the contents of the pattern and hold spaces.</dd>
|
|
|
|
<dt><strong>
|
|
[<em>2addr</em>]y/<em>string1</em>/<em>string2</em>/</strong></dt>
|
|
|
|
<dd>Replace all occurrences of characters in <em>string1</em> in
|
|
the pattern space with the corresponding characters from <em>
|
|
string2</em>. Any character other than a backslash or newline can
|
|
be used instead of a slash to delimit the strings. Within <em>
|
|
string1</em> and <em>string2</em>, a backslash followed by any
|
|
character other than a newline is that literal character, and a
|
|
<strong>/n</strong> is replaced by a newline character.</dd>
|
|
|
|
<dt><strong>[<em>2addr</em>]!<em>function</em></strong></dt>
|
|
|
|
<dt><strong>[<em>2addr</em>]!<em>function-list</em></strong></dt>
|
|
|
|
<dd>Apply the <em>function</em> or <em>function-list</em> only to
|
|
the lines that are <strong>not</strong> selected by the
|
|
address(es).</dd>
|
|
|
|
<dt><strong>[<em>0addr</em>]:<em>label</em></strong></dt>
|
|
|
|
<dd>This function does nothing; it bears a <em>label</em> to which
|
|
the <strong>b</strong> and <strong>t</strong> commands may
|
|
branch.</dd>
|
|
|
|
<dt><strong>[<em>1addr</em>]=</strong></dt>
|
|
|
|
<dd>Write the line number to the standard output followed by a
|
|
newline character.</dd>
|
|
|
|
<dt><strong>[<em>0addr</em>]</strong></dt>
|
|
|
|
<dd>Empty lines are ignored.</dd>
|
|
|
|
<dt><strong>[<em>0addr</em>]#</strong></dt>
|
|
|
|
<dd>The <strong>#</strong> and the remainder of the line are
|
|
ignored (treated as a comment), with the single exception that if
|
|
the first two characters in the file are <strong>#n</strong>, the
|
|
default output is suppressed. This is the same as specifying the -n
|
|
option on the command line.</dd>
|
|
</dl>
|
|
|
|
|
|
<p><strong>Environment Variables</strong></p>
|
|
|
|
<p><strong>sed</strong> is affected by the following environment
|
|
variables:</p>
|
|
|
|
|
|
<dl>
|
|
<dt><strong>QIBM_CCSID</strong></dt>
|
|
|
|
<dd>Any files created by <strong>sed</strong> are created with the CCSID specified by
|
|
the value of the environment variable.</dd>
|
|
</dl>
|
|
|
|
|
|
<p><strong>Exit Status</strong></p>
|
|
|
|
<ul>
|
|
<li>0 on success</li>
|
|
|
|
<li>>0 if an error occurs</li>
|
|
</ul>
|
|
|
|
<p><strong>Related information</strong></p>
|
|
|
|
<ul>
|
|
<li><a href="cmp.htm">cmp - Compare two files</a></li>
|
|
|
|
<li><a href="sort.htm">sort - Sort, merge, or sequence check text
|
|
files</a></li>
|
|
|
|
<li><a href="split.htm">split - Split files into pieces</a></li>
|
|
|
|
<li><a href="uniq.htm">uniq - Report or filter out repeated lines
|
|
in a file</a></li>
|
|
</ul>
|
|
|
|
|
|
<!-- end ibmbody========================================================= -->
|
|
</body>
|
|
</html>
|
|
|