|
|
July
21, 2003 -
A Brief Introduction to sed
|
Unlike
the UNIX editors vi and emacs, sed is a
"non-interactive"
stream-oriented editor. This
"non-interactive" trait allows
you to use sed to automate editing
sequences if desired. The name sed
is an abbreviation for stream editor,
and the utility derives many of its
commands from the ed line-editor (ed was
the first UNIX text editor).
The power and usefulness of sed can be
seen when the same edit, or a series of
edits, has to be performed multiple times
in a single file, or one or more times in
multiple files. Imagine having to
use vi interactively to make the same
changes to 100 different files.
sed commands can be issued on the command
line using the following syntax:
|
sed [-e]
'command1' [-e command2...] [file]
|
Notice that
more than one sed command can be performed
by prefacing each command with the
"-e" option. This option
is not required if only one command is
used. Specifying a file is optional
because sed can take its input from either
files or stdin (standard input).
Similar to a UNIX shell script, multiple
sed commands may also be stored in a
script file. The "-f"
option is used on the command line to
access the commands in the script:
|
sed -f
script [file]
|
The follow text
from our home page will be used to
demonstrate a frequently-performed sed
operation, text substitution (replacement):
|
LiveFire Labs specializes in providing quality, affordable, and globally
accessible hands-on UNIX training to students who are serious about learning
the UNIX operating system and related technologies. UNIX technologists develop
and support our courses, and each course is designed to take advantage of the
company's innovative hands-on training model.
|
|
For this
week's examples, this block of text will be
stored in a file named LFL.
Consider the following command and its
stdout (standard output):
|
$ sed 's/UNIX/unix/' LFL
LiveFire Labs specializes in providing quality, affordable, and globally
accessible hands-on unix training to students who are serious about learning
the unix operating system and related technologies. UNIX technologists develop
and support our courses, and each course is designed to take advantage of the
company's innovative hands-on training model.
|
The s
command replaces the first occurrence of
UNIX on each line in the file with unix.
Adding g to the end of the s command
will instruct sed to perform a global
substitution (all instances of UNIX will be
replaced with unix):
|
$ sed 's/UNIX/unix/g' LFL
LiveFire Labs specializes in providing quality, affordable, and globally
accessible hands-on unix training to students who are serious about learning
the unix operating system and related technologies. unix technologists develop
and support our courses, and each course is designed to take advantage of the
company's innovative hands-on training model.
|
This extremely
brief introduction to sed does not even
begin to scratch the surface on its
capabilities, but you should now have a
basic understanding of what sed is, how it
differs from other UNIX editors, and when it
may be appropriate to use the utility for
editing text.
|
|
|
Learn
more...
If you are new to the UNIX or Linux
operating system and would like to learn
more, you may want to consider
registering for LiveFire Labs' UNIX
and Linux Operating System Fundamentals
online training course.
If you already have a solid grasp of the
fundamentals but would like to learn more
about the Korn shell and basic and
advanced shell scripting, taking our Korn
Shell Scripting course will be
beneficial to you.
Our innovative hands-on training model
allows you to learn
UNIX by completing hands-on
exercises on real servers in our Internet
Lab.
More
Tips...
· Popular
UNIX Tips from the Past
|
|
|
|
 |
 |
| |
Receive
the UNIX Tip, Trick, or Shell Script of the
Week by Email
|
|
|
|