http://wiki.christophchamp.com/index.php?title=Sed/manpage&feed=atom&action=history
Sed/manpage - Revision history
2024-03-29T06:27:56Z
Revision history for this page on the wiki
MediaWiki 1.26.2
http://wiki.christophchamp.com/index.php?title=Sed/manpage&diff=2507&oldid=prev
Christoph at 05:58, 7 August 2006
2006-08-07T05:58:35Z
<p></p>
<p><b>New page</b></p><div>'''sed''' - manual page for [[sed]](1) version 4.1.3 (January 2005)<br />
<br />
== Synopsis ==<br />
<br />
sed [OPTION]... {script-only-if-no-other-script} [input-file]...<br />
<br />
== Description ==<br />
<br />
Sed 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 ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.<br />
<br />
* suppress automatic printing of pattern space:<br />
-n, --quiet, --silent<br />
<br />
* add the script to the commands to be executed:<br />
-e script, --expression=script<br />
<br />
* add the contents of script-file to the commands to be executed:<br />
-f script-file, --file=script-file<br />
<br />
* edit files in place (makes backup if extension supplied):<br />
-i[SUFFIX], --in-place[=SUFFIX]<br />
<br />
* specify the desired line-wrap length for the '<tt>l</tt>' command<br />
-l N, --line-length=N<br />
<br />
* disable all GNU extensions:<br />
--posix<br />
<br />
* use extended regular expressions in the script:<br />
-r, --regexp-extended<br />
<br />
* consider files as separate rather than as a single continuous long stream:<br />
-s, --separate<br />
<br />
* load minimal amounts of data from the input files and flush the output buffers more often:<br />
-u, --unbuffered<br />
<br />
* display this help and exit:<br />
--help<br />
<br />
* output version information and exit:<br />
--version<br />
<br />
If no <tt>-e</tt>, <tt>--expression</tt>, <tt>-f</tt>, or <tt>--file</tt> 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.<br />
<br />
== Command synopsis ==<br />
<br />
This is just a brief synopsis of sed commands to serve as a reminder to those who already know sed; other documentation (such as the texinfo document) must be consulted for fuller descriptions.<br />
<br />
=== Zero-address "commands" ===<br />
<br />
* Label for <tt>b</tt> and <tt>t</tt> commands:<br />
: label<br />
<br />
* The comment extends until the next newline (or the end of a <tt>-e</tt> script fragment):<br />
#comment<br />
<br />
* The closing bracket of a <tt>{ }</tt> block:<br />
}<br />
<br />
=== Zero- or one-address commands ===<br />
<br />
* Print the current line number:<br />
=<br />
<br />
* Append text, which has each embedded newline preceded by a backslash:<br />
a \<br />
text<br />
<br />
* Insert text, which has each embedded newline preceded by a backslash:<br />
i \<br />
text<br />
<br />
* Immediately quit the sed script without processing any more input, except that if auto-print is not disabled the current pattern space will be printed:<br />
q<br />
<br />
* Immediately quit the sed script without processing any more input:<br />
Q<br />
<br />
* Append text read from filename:<br />
r filename<br />
<br />
* Append a line read from filename:<br />
R filename<br />
<br />
=== Commands which accept address ranges ===<br />
<br />
* Begin a block of commands (end with a <tt>}</tt>):<br />
{<br />
<br />
* Branch to label; if label is omitted, branch to end of script:<br />
b label<br />
<br />
* If a <tt>s///</tt> has done a successful substitution since the last input line was read and since the last <tt>t</tt> or <tt>T</tt> command, then branch to label; if label is omitted, branch to end of script:<br />
t label<br />
<br />
* If no <tt>s///</tt> has done a successful substitution since the last input line was read and since the last <tt>t</tt> or <tt>T</tt> command, then branch to label; if label is omitted, branch to end of script:<br />
T label<br />
<br />
* Replace the selected lines with text, which has each embedded newline preceded by a backslash:<br />
c \<br />
text<br />
<br />
* Delete pattern space. Start next cycle:<br />
d<br />
<br />
* Delete up to the first embedded newline in the pattern space. Start next cycle, but skip reading from the input if there is still data in the pattern space:<br />
D<br />
<br />
* Copy/append pattern space to hold space:<br />
h H<br />
<br />
* Copy/append hold space to pattern space:<br />
g G<br />
<br />
* Exchange the contents of the hold and pattern spaces:<br />
x<br />
<br />
* List out the current line in a "visually unambiguous" form:<br />
l<br />
<br />
* Read/append the next line of input into the pattern space:<br />
n N<br />
<br />
* Print the current pattern space:<br />
p<br />
<br />
* Print up to the first embedded newline of the current pattern space:<br />
P<br />
<br />
* Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character <tt>&</tt> to refer to that portion of the pattern space which matched, and the special escapes <tt>\1</tt> through <tt>\9</tt> to refer to the corresponding matching sub-expressions in the regexp:<br />
s/regexp/replacement/<br />
<br />
* Write the current pattern space to filename:<br />
w filename<br />
<br />
* Write the first line of the current pattern space to filename:<br />
W filename<br />
<br />
* Transliterate the characters in the pattern space which appear in <tt>source</tt> to the corresponding character in <tt>dest</tt>:<br />
y/source/dest/<br />
<br />
=== Addresses ===<br />
<br />
Sed 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 <tt>addr1,addr2</tt> (i.e., the addresses are separated by a comma); the line which <tt>addr1</tt> matched will always be accepted, even if <tt>addr2</tt> selects an earlier line; and if <tt>addr2</tt> is a regexp, it will not be tested against the line that <tt>addr1</tt> matched.<br />
<br />
After the address (or address-range), and before the command, a <tt>!</tt> may be inserted, which specifies that the command shall only be executed if the address (or address-range) does not match.<br />
<br />
==== The following address types are supported ====<br />
<br />
* Match only the specified line number:<br />
number<br />
<br />
* Match every <tt>step</tt>'th line starting with line first. For example, "<tt>sed -n 1~2p</tt>" will print all the odd-numbered lines in the input stream, and the address <tt>2~5</tt> will match every fifth line, starting with the second. (This is an extension.):<br />
first~step<br />
<br />
* Match the last line:<br />
$<br />
<br />
* Match lines matching the regular expression <tt>regexp</tt>:<br />
/regexp/<br />
<br />
* Match lines matching the regular expression regexp. The '''<tt>c</tt>''' may be any character:<br />
\'''c'''regexp'''c'''<br />
<br />
==== GNU sed also supports some special 2-address forms ====<br />
<br />
* Start out in "matched first address" state, until <tt>addr2</tt> is found. This is similar to <tt>1,addr2</tt>, except that if <tt>addr2</tt> matches the very first line of input the <tt>0,addr2</tt> form will be at the end of its range, whereas the <tt>1,addr2</tt> form will still be at the beginning of its range:<br />
0,addr2<br />
<br />
* Will match <tt>addr1</tt> and the <tt>N</tt> lines following <tt>addr1</tt>:<br />
addr1,+N<br />
<br />
* Will match <tt>addr1</tt> and the lines following <tt>addr1</tt> until the next line whose input line number is a multiple of <tt>N</tt>:<br />
addr1,~N<br />
<br />
== [[Regular expression]]s ==<br />
<br />
POSIX.2 BREs should be supported, but they aren't completely because of performance problems. The <tt>\n</tt> sequence in a regular expression matches the newline character, and similarly for <tt>\a</tt>, <tt>\t</tt>, and other sequences.<br />
<br />
== Copyright ==<br />
Copyright © 2003 Free Software Foundation, Inc.<br />
<br />
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law.<br />
<br />
== See also ==<br />
<br />
* [[awk]]<br />
* [[ed]]<br />
* [[grep]]<br />
* [[tr]]<br />
* [[perlre]]<br />
* sed.info<br />
* any of various books on sed, the sed FAQ (http://sed.sf.net/grabbag/tutorials/sedfaq.html), http://sed.sf.net/grabbag/.<br />
<br />
Note: The full documentation for sed is maintained as a Texinfo manual. If the info and sed programs are properly installed at your site, the command<br />
info sed<br />
should give you access to the complete manual.<br />
<br />
[[Category:Linux Command Line Tools]]<br />
[[Category:Scripting languages]]</div>
Christoph