sed(1) sed(1)
NAME [Toc] [Back]
sed - stream text editor
SYNOPSIS [Toc] [Back]
sed [-n] script [file ...]
sed [-n] [-e script] ... [-f script_file] ... [file ...]
DESCRIPTION [Toc] [Back]
sed copies the named text files (standard input default) to the
standard output, edited according to a script containing up to 100
commands. Only complete input lines are processed. Any input text at
the end of a file that is not terminated by a new-line character is
ignored.
Options [Toc] [Back]
sed recognizes the following options:
-f script_file Take script from file script_file.
-e script Edit according to script. If there is just one -e
option and no -f options, the flag -e can be
omitted.
-n Suppress the default output.
sed interprets all -escript and -fscript_file arguments in the order
given. Use caution, if mixing -e and -f options, to avoid
unpredictable or incorrect results.
Command Scripts [Toc] [Back]
A script consists of editor commands, one per line, of the following
form:
[address [, address]] function [arguments]
In normal operation, sed cyclically copies a line of input into a
pattern space (unless there is something left after a D 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 -n) and deletes the pattern space.
Some of the commands use a hold space to save all or part of the
pattern space for subsequent retrieval.
Command Addresses [Toc] [Back]
An address is either a decimal number that counts input lines
cumulatively across files, a $ which addresses the last line of input,
or a context address; that is, a /regular expression/ in the style of
ed(1) modified thus:
Hewlett-Packard Company - 1 - HP-UX 11i Version 2: August 2003
sed(1) sed(1)
+ In a context address, the construction \?regular expression?,
where ? is any character, is identical to /regular
expression/. Note that in the context address \xabc\xdefx,
the second x stands for itself, so that the regular expression
is abcxdef.
+ The escape sequence \n matches a new-line character embedded
in the pattern space.
+ A period (.) matches any character except the terminal newline
of the pattern space.
+ A command line with no addresses selects every pattern space.
+ A command line with one address selects each pattern space
that matches the address.
+ 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 one line is selected). Thereafter
the process is repeated, looking again for the first address.
sed supports Basic Regular Expression syntax (see regexp(5)).
Editing commands can also be applied to only non-selected pattern
spaces by use of the negation function ! (described below).
Command Functions [Toc] [Back]
In the following list of functions, the maximum number of permissible
addresses for each function is indicated in parentheses. Other
function elements are interpreted as follows:
text One or more lines, all but the last of which end with
\ to hide the new-line. Backslashes in text are
treated like backslashes in the replacement string of
an s command, and can be used to protect initial
blanks and tabs against the stripping that is done on
every script line.
rfile Must terminate the command line, and must be preceded
by exactly one blank.
wfile Must terminate the command line, and must be preceded
by exactly one blank. Each wfile is created before
processing begins. There can be at most 10 distinct
wfile arguments.
sed recognizes the following functions:
Hewlett-Packard Company - 2 - HP-UX 11i Version 2: August 2003
sed(1) sed(1)
(1)a\
text Append. Place text on the output before reading next
input line.
(2)b label Branch to the : command bearing label. If no label is
specified, branch to the end of the script.
(2)c\
text Change. Delete the pattern space. With 0 or 1 address or
at the end of a 2-address range, place text on the output.
Start the next cycle.
(2)d Delete pattern space and start the next cycle.
(2)D Delete initial segment of pattern space through first
new-line and start the next cycle.
(2)g Replace contents of the pattern space with contents of the
hold space.
(2)G Append contents of hold space to the pattern space.
(2)h Replace contents of the hold space with contents of the
pattern space.
(2)H Append the contents of the pattern space to the hold
space.
(1)i\
text Insert. Place text on the standard output.
(2)l List the pattern space on the standard output in an
unambiguous form. Non-printing characters are spelled in
three-digit octal number format (with a preceding
backslash), and long lines are folded.
(2)n Copy the pattern space to the standard output if the
default output has not been suppressed (by the -n option
on the command line or the #n command in the script file).
Replace the pattern space with the next line of input.
(2)N Append the next line of input to the pattern space with an
embedded new-line. (The current line number changes.)
(2)p Print. Copy the pattern space to the standard output.
(2)P Copy the initial segment of the pattern space through the
first new-line to the standard output.
(1)q Quit. Branch to the end of the script. Do not start a
new cycle.
Hewlett-Packard Company - 3 - HP-UX 11i Version 2: August 2003
sed(1) sed(1)
(1)r rfile Read contents of rfile and place on output before reading
the next input line.
(2)s/regular expression/replacement/flags
Substitute replacement string for instances of regular
expression in the pattern space. Any character can be
used instead of /. For a fuller description see ed(1).
flags is zero or more of:
n n=1-2048 (LINE_MAX). Substitute for just
the nth occurrence of regular expression in
the pattern space.
g Global. Substitute for all non-overlapping
instances of regular expression rather than
just the first one.
p Print the pattern space if a replacement
was made and the default output has been
suppressed (by the -n option on the command
line or the #n command in the script file).
w wfile Write. Append the pattern space to wfile
if a replacement was made.
(2)t label Test. Branch to the : command bearing the label if any
substitutions have been made since the most recent reading
of an input line or execution of a t. If label is empty,
branch to the end of the script.
(2)w wfile Write. Append the pattern space to wfile.
(2)x Exchange the contents of the pattern and hold spaces.
(2)y/string1/string2/
Transform. Replace all occurrences of characters in
string1 with the corresponding character in string2. The
lengths of string1 and string2 must be equal.
(2)! function
Don't. Apply the function (or group, if function is {)
only to lines not selected by the address or addresses.
(0): label This command does nothing; it bears a label for b and t
commands to branch to.
(1)= Place the current line number on the standard output as a
line.
(2){ Execute the following commands through a matching } only
when the pattern space is selected. The syntax is:
Hewlett-Packard Company - 4 - HP-UX 11i Version 2: August 2003
sed(1) sed(1)
{ cmd1
cmd2
cmd3
.
.
.
}
(0) An empty command is ignored.
(0)# If a # appears as the first character on the first line of
a script file, that entire line is treated as a comment
with one exception: If the character after the # is an n,
the default output is suppressed. The rest of the line
after #n is also ignored. A script file must contain at
least one non-comment line.
EXTERNAL INFLUENCES [Toc] [Back]
Environment Variables
LANG provides a default value for the internationalization variables
that are unset or null. If LANG is unset or null, the default value of
"C" (see lang(5)) is used. If any of the internationalization
variables contains an invalid setting, sed will behave as if all
internationalization variables are set to "C". See environ(5).
LC_ALL If set to a non-empty string value, overrides the values of all
the other internationalization variables.
LC_CTYPE determines the interpretation of text as single and/or
multi-byte characters, the classification of characters as printable,
and the characters matched by character class expressions in regular
expressions.
LC_MESSAGES determines the locale that should be used to affect the
format and contents of diagnostic messages written to standard error
and informative messages written to standard output.
NLSPATH determines the location of message catalogues for the
processing of LC_MESSAGES.
International Code Set Support [Toc] [Back]
Single- and multi-byte character code sets are supported.
EXAMPLES [Toc] [Back]
Make a simple substitution in a file from the command line or from a
shell script, changing abc to xyz:
sed 's/abc/xyz/' file1 >file1.out
Same as above but use shell or environment variables var1 and var2 in
search and replacement strings:
Hewlett-Packard Company - 5 - HP-UX 11i Version 2: August 2003
sed(1) sed(1)
sed "s/$var1/$var2/" file1 >file1.out
or
sed 's/'$var1'/'$var2'/' file1 >file1.out
Multiple substitutions in a single command:
sed -e 's/abc/xyz/' -e 's/lmn/rst/' file1 >file1.out
or
sed -e 's/abc/xyz/' \
-e 's/lmn/rst/' \
file1 >file1.out
WARNINGS [Toc] [Back]
sed limits command scripts to a total of not more than 100 commands.
The hold space is limited to 8192 characters.
sed processes only text files. See the glossary for a definition of
text files and their limitations.
AUTHOR [Toc] [Back]
sed was developed by OSF and HP.
SEE ALSO [Toc] [Back]
awk(1), ed(1), grep(1), environ(5), lang(5), regexp(5).
sed: A Non-Interactive Streaming Editor tutorial in the Text
Processing Users Guide.
STANDARDS CONFORMANCE [Toc] [Back]
sed: SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2
Hewlett-Packard Company - 6 - HP-UX 11i Version 2: August 2003 [ Back ] |