The 1990's model of ed in cLIeNUX behaves quite like the one documented in 1971. ed has a bit of a learning threshhold, but then you notice that ed still basically kicks butt. sed, grep, vi and various other unix utilities are largely just different interfaces to the functionality that exists (and was pre-existing) in ed. The added functionality in newer versions, such as the j command, mostly retains the power and simplicity of the original. This mini-tutorial though, is based on the features available in 1971, and refers to the 1971 manual page.
Let's say you have a text file to edit named "tfile.html". tfile.html is
in some format, say HTML, and you want to use an editor with powerful
commands to automagically remove all the HTML markup tags. Do
ed tfile.html
(I'm going to assume you know to hit the enter key.) That will start ed,
load tfile.html into ed's buffer, and leave ".", ed's current line
address, at the last line of tfile.html (actually the buffer containing
what is currently a complete copy of tfile.html). Do = and ed will tell
you the number of lines in the buffer, which is the number of lines in the
file. If you just hit return, ed will give you it's ? error message,
because since you are currently pointed at the last line in the file, the
enter key, the "newline command", tells ed to print the next line, a line
that doesn't exist. Well heck, let's look at the thing. Do
1
Now if you hold down the enter key you will scroll through the buffer,
viewing the current state of your copy of tfile.html. Stop scrolling at
some point and do
.=
and ed will tell you the line number of ".", your current position.
Say there's a bunch of lines at the top of tfile.html that you want to get
rid of regardless of the HTML in them. Use line number addressing,
scrolling, and .= to put yourself at the first line you want to keep. Then
do
.,$w tfile.n1
to write a new file truncated as you wish, from your current position to
the end of the file/buffer. Now do
e tfile.n1
and have a look at the results. You can do the above steps over and over
till you get it right, as long as you don't w to your original tfile.html.
OK, you got rid of the big obvious change, now you need to figure out how
to un-html what's left, which is a bit trickier. First let's just try to
look at all the tags with
/<.*>/p
regular expression print. That should show all the lines with entire HTML
tags on one line, but there are likely to be other tags that aren't all on
one line. If the html was written so that all tags were alone on a line
you could unhtml the lot with
/<.*>/d
but that's seldom the case. You might get lucky that way with *roff stuff
though, for example. You can do much more elegant things than the above
examples, with regular expressions as addresses and other facilites. The
emphasis in ed, and in unix, is to provide you with the tools, and let you
decide how to use them. That's why the original documentation above is a
bit dry. It tells you how ed works, not what it's for. That has proven to
be a very good approach, but a few things bear a bit of clarification.
The bit about null characters in regular expressions, for one. What that
means for example in the case of the ^ is a regex (regular expression)
that starts with a ^ will match if the rest of the regex matches the
beginning of a line. ^ by itself will match every line. ^[A-Z] will match
every line that starts with a capital letter. Similarly, [0-9]$ will match
every line that ends in a numeral. If you do a substitution involving ^
the replacement text will go on the beginning of the lines effected.
/ppendix/,$s/^/>/
That command will add a > to the beginning of each line of the GNU GPL
Version 2, from the line containing "Appendix", to the end, much as a
mailer would to with a quoted message. The above command consists of
The bit about & is a bit confusing too. Let's make Stallman stutter.
ed COPYING
again, and do
,s/ .. /&&&/
and most two-letter words in the GPL will be duplicated three times in
succession. Here the comma is shorthand for "every line", and the
replacement pattern is 3 instances of the string the search pattern found.
Now do ,p to see the damage you've done.
The g command, "global", is how you make things happen numerous times in ed with one invocation.
cLIeNUX edtut.1.html Copyright 1998 Rick Hohensee (Richard Allen) released for redistribution only as part of cLIeNUX as a whole