sedstream editor (non-interactive) |
Command |
sed
[-En
] script [file ...]
sed
[-En
]
[-e
script] ...
[-f
scriptfile] ...
[file ...]
sed
command applies a set of editing commands contained
in script to each input file; if more than one file is
specified, they are concatenated and treated as a single large file.
script is the collective arguments of all -e
options
and the contents of all scriptfiles. You can specify multiple
-e
and -f
options. Commands are added to
script in the order specified. If you did not specify a file,
sed
reads the standard input.
sed
reads each input line into a special area known as the
pattern buffer. Certain commands (gGhHx
) use a second
area called the hold buffer. By default, after each pass through the
script, sed
writes the final contents of the pattern
buffer to the standard output.
-E
uses extended regular expressions. Normally, sed
uses Basic regular expressions. For more information, see
regexp
.
-e
scriptadds the editing commands script to the end of the script.
-f
scriptfileadds the commands in the file scriptfile (one command per line) to end of the script.
-n
suppresses default output; output generated by explicit commands
(acilpPsr=
) is still sent to standard output.
-e
and use the first form of the command.
sed
commands are similar to those of the interactive text
editor ed
, except that
sed
commands view the input text as a stream rather than as
a directly addressable file.
Each line of a sed
script contains up to two addresses, a
single letter command, possible command modifiers and a terminating newline. The
newline is optional in script strings typed on the command line.
[addr[,addr]] command [modifiers]
The number n matches only the nth input line.
$
This address matches the last input line.
/
regexp/
This address selects an input line matching the specified regular
expression regexp. You can use any printable character other than
the slash (/
) to delimit the regular expression by putting a
backslash (\
) before the first one. For example, to use
%
to enclose the regular expression, write
\%
regexp%
.
sed
command with
the maximum number of legitimate addresses. A command may be given fewer than
the number of addresses specified, but not more.
a\
appends subsequent text lines from the script to the standard output.
sed
outputs the text before reading the next record.
Text lines are terminated by the first line that does not end with a
backslash (\
). sed
does not treat
the \
characters on the end of lines as part of the
text.
If used on a command line under the MKS KornShell, sed
a\
requires that you include a newline character. You
must either enclose the entire text in single quotes
(''
) or use two backslashes together. For example, you
could use either
sed -e 'a\^Jtext' file
or
sed -e "a\\^Jtext" file
b
[label]branches to label. If you omit label,
sed
branches to the end of the script.
c
\
change the addressed lines by deleting the contents of the pattern
buffer (input line) and sending subsequent text (similar to the
a
command) to the standard output. When you specify
two addresses, sed
delays text output until the final
line in the range of addresses; otherwise, the behavior would surprise
many users. The rest of the script is skipped for each addressed line,
except the last.
d
deletes the contents of the pattern buffer (input line) and restarts the script with the next input line.
D
deletes the pattern buffer up to and including the first newline, and then restarts the script from the beginning and applies it to the remaining text in the pattern buffer.
g
grabs a copy of the text in the hold buffer and places it in the pattern buffer, overwriting the original contents.
G
grabs a copy of the text in the hold buffer and appends it to the end of the pattern buffer after appending a newline.
h
holds a copy of the text in the pattern buffer by placing it in the hold buffer, overwriting its original contents.
H
holds a copy of the text in the pattern buffer by appending it to the end of the hold buffer after appending a newline.
i\
inserts text in the output. This command is similar to the
a
command, except that its text is output
immediately.
l
lists (prints) the pattern buffer (input line) onto the standard output
so that non-printable characters are visible. The end-of-line is
represented by $
, and the characters \\
,
\a
, \b
, \f
, \r
,
\t
and \v
are printed as escape sequences. This
command is analogous to the l
command in
ed
. sed
folds
long lines to suit the output device, indicating the point of folding with
a backslash (\
).
n
prints the pattern space on standard output if the default printing of
the pattern space is not suppressed (with of the -n
option). The next line of input is then read, and the processing of
the line continues from the location of the n
command
in the script.
N
appends the next line of input to the end of the pattern buffer, using a newline to separate the appended material from the original. The current line number changes.
p
prints the text in the pattern buffer to the standard output. The
-n
option does not disable this form of output. If you
do not use -n
, the pattern buffer is printed twice.
P
operates like the p
command, except that it prints
the text in the pattern buffer only up to and including the first newline
character.
q
quits sed
, skipping the rest of the script and
reading no more input lines.
r
filereads text from file and places it onto the standard output
before reading the next input line. The timing of this operation is the
same as for the a
command. If file does not
exist or cannot be read, sed
treats it as an empty
file.
s/
reg/
sub/
[gp
n][w
file]substitutes the text string sub for text matching the regular
expression reg. Normally, the s
command
replaces only the first such matching string in each input line. You can
use any single printable character other than space or newline instead of
the slash (/
) to delimit reg and sub. The
delimiter itself may appear as a literal character in reg or
sub if you precede it with a backslash (\
).
If an ampersand (&
) appears in sub,
sed
replaces it with the string matching reg.
For more about special characters in regular expressions, see
regexp
. A \n
in
reg matches an embedded newline in the pattern buffer (resulting,
for example, from an N
command).
The command may be followed by a combination of the following:
g
p
p
) command only if a
successful substitution occurs.w
filet
[label]branches to the indicated label if any successful substitution
has occurred since either reading the last input line or executing the
last t
command. If you do not specify label,
sed
branches to the end of the script.
w
filewrites the text in the pattern buffer to the end of file.
x
exchanges the text in the hold buffer with that in the pattern buffer.
y/
set1/
set2/
transliterates any input character occurring in set1 to the corresponding element of set2. The sets must be the same length. You can use any character other than backslash or newline instead of the slash, to delimit the strings.
{
groups all commands until the next matching }
, so
that sed
executes the entire group only if the
{
command is selected by its address(es).
:
labeldesignates a label that can be the destination of a
b
or t
command.
!
cmdexecutes the specified sed
cmd only on lines
not selected by the addresses.
#
treats the script line as a comment unless it is the first line in the
script. If the first line in a script is #n
, it is equivalent
to specifying -n
on the command line. Any empty script
line is also treated as a comment.
=
writes the decimal value of the current line number onto the standard output.
This filter converts backslashes to forward slashes:sed 's/democrat\(ic\)*/republican/g'
sed 's#\\#/#g'
COLUMNS
contains the width of the screen in columns. If set,
sed
uses this value to fold long lines on output;
otherwise, sed
uses the appropriate value from the
terminal database. If that is not valid, sed
uses a
default of 80.
0
Successful completion.
1
Failure because of any of the following:
!
command\
must terminate commandy
command must be followed by a printable
character as separator{
and }
commandsG
commandH
commandThe given command required a file name, but its operand did not have the syntax of a file name.
A !
command cannot contain a !
command of its
own.
The specified command required a label, but you did not supply one.
The input to sed
must contain at least one active
command (that is, a command that is not a comment).
You issued a command that tried to use a remembered regular expression,
for example, s//abc
; however, there is no remembered regular
expression yet. To correct this, change the command to use an explicit
regular expression.
sed
allows a limit of 40 kilobytes per line. It does not
allow the NUL character. The maximum length of a global command is 256
characters, including newlines.
-E
option is an extension to the POSIX and x/OPEN
standards.
regexp