Chapter 12. The sed Editor
sed is a noninteractive (stream-oriented) editor that interprets and performs the
actions in a script. sed is said to
be stream-oriented because, as with many Unix programs, input flows
through the program and is directed to standard output. For example,
sort is stream-oriented; vi is not. sed's input typically comes
from a file but can be directed from the keyboard. Output goes to the
screen by default but can be captured in a file instead. Typical uses of sed include: -
Editing one or more files automatically
-
Simplifying repetitive edits to multiple files
-
Writing conversion programs
sed operates as follows: -
Each line of input is copied into a pattern space.
-
All editing commands in a sed script
are applied in order to each line of input.
-
Editing commands are applied to all lines (globally) unless line
addressing restricts the lines affected.
-
If a command changes the input, subsequent commands are applied to
the changed line, not to the original input line.
-
The original input file is unchanged because the editing commands
modify a copy of the original input line. The copy is sent to
standard output (but can be redirected to a file).
For more information on sed, see
sed & awk (O'Reilly).
12.1. Command-Line Syntax
The syntax for invoking sed has two
forms:
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)
The first form allows you to specify an editing command, surrounded
by single quotes, on the command line. The second form allows you to
specify a scriptfile, which is a file containing
sed commands. If no files are
specified, sed reads from standard
input.
The following options are recognized:
- -e cmd, --expression=cmd
-
Next argument is an editing command; not needed unless specifying two
or more editing commands.
- -f scriptfile, --file=scriptfile
-
Next argument is a file containing editing commands.
- -n, --silent, --quiet
-
Suppress the default output; sed
displays only those lines specified with the p command or with the p flag of the s command.
- -V, --version
-
Display version number.
- -h, --help
-
Display brief help message with command-line options.
| | | 11.15. vi Configuration | | 12.2. Syntax of sed Commands |
Copyright © 2003 O'Reilly & Associates. All rights reserved.
|
|