Open System Services Shell and Utilities Reference Manual (G06.28+, H06.05+)

sed(1) OSS Shell and Utilities Reference Manual
or tabs. The list of subcommands must be separated by newline characters. The sub-
commands can also be preceded by spaces or tabs. The terminating } (right brace)
must be preceded by a newline character and then zero or more spaces.
(0):label This script entry simply marks a branch point to be referenced by the b and t com-
mands. This label can be any sequence of eight or fewer bytes.
(1)= Writes the current line number to standard output as a line.
(0) Ignores an empty command.
(0)# If a # (number sign) appears as the rst character on a line, that entire line is treated as
a comment, with one exception. If the rst two characters of a script le are #n, the
default output is suppressed (same as -n).
EXAMPLES
1. To perform a global change, enter:
sed "s/happy/enchanted/g" chap1 >chap1.new
This replaces each occurrence of happy found in the le chap1 with enchanted, and
puts the edited version in a separate le named chap1.new. The g at the end of the s sub-
command tells sed to make as many substitutions as possible on each line. Without the
g, sed replaces only the rst happy on a line.
The sed stream editor operates as a lter. It reads text from standard input or from the
les named on the command line (chap1 in this example), modies this text, and writes it
to standard output. Unlike most editors, it does not replace the original le. This makes
sed a powerful command when used in pipelines.
2. To use sed as a lter in a pipeline (sh only), enter:
pr chap2 | sed "s/Page *[0-9]*$/(&)/" | print
This encloses the page numbers in parentheses before printing chap2. The pr command
puts a heading and page number at the top of each page, then sed puts the page numbers
in parentheses, and the print command prints the edited listing.
The sed pattern /Page *[0-9]*$/ matches page numbers that appear at the end of a line.
The s subcommand changes this to (&), where the & stands for the pattern that was
matched (for example, Page 5).
3. To display selected lines of a le, enter:
sed -n "/food/p" chap3
This displays each line in chap3 that contains the word food. Normally, sed copies
every line to standard output after it is edited. The -n ag stops sed from doing this. You
then use subcommands like p to write specic parts of the text. Without the -n, this
example displays all the lines in chap3, and it shows each line containing food twice.
4. To perform complex editing, enter:
sed -f script.sed chap4 >chap4.new
It is always a good idea to create a sed script le when you want to do anything complex.
You can then test and modify your script before using it. You can also reuse your script
to edit other les. Create the script le with an interactive text editor.
86 Hewlett-Packard Company 527188-007