Grex Jellyware Conference

Item 115: Unix tool o' the day.

Entered by cross on Sat Sep 18 06:28:48 2010:

1 new of 22 responses total.


#7 of 22 by cross on Wed Sep 22 12:33:20 2010:

Today's Unix tool o' the day is: ed.  The Standard Editor.

And that's what it is: ed is a line oriented text editor.  One uses it 
by running 'ed file' and then typing in commands from the ed command 
set.  See the ed(1) manual page, or the book, "The Unix Programming 
Environment" for information on how to use ed.

Ed is not a graphical editor; it does not display a full screen-full 
of information at a time.  In fact, it won't display any part of the 
file unless you tell it to by running special ed commands; ed was 
designed for use on printing teletype terminals (ie, terminals that 
output to an actual typewriter-like device, instead of a graphic 
screen).  Its diagnostics are horrible in their simplicity: usually, 
if you do something ed does not understand, you get a line with the 
single character '?' on it, all by itself.  Some have called it 
the, "What you get is what you get" or WYGIWYG text editor.

Ed is probably the first Unix text editor.  I say "probably" because 
it's certainly been rewritten several times, and they tried several 
variants out at Bell Labs.  It is descended from the QED editor that 
was used on the Multics system and CTSS.  Ed was implemented mostly by 
Ken Thompson in the early 1970s, and went on to inspire the Sam and 
acme text editors written by Rob Pike.  It is called "the Standard 
Editor" because it was the only traditional text editor that came with 
early Unix distributions.

So, ed is not graphical, has horrible diagnostics, an arcane command 
set, and was designed for 1960s standards, why on earth would anyone 
want to learn it now?

Because it's tremendously useful as an editing tool for in-place 
files.  Given that it reads commands from standard input, it can also 
be used as a target in a pipeline: one can write scripts that generate 
ed commands that are then piped into ed running on a file; this is 
handy for batch-mode editing without using temporary files (ed might 
make a temporary file, but the script-writer won't have to worry about 
that).

Have you got 1000 files in a directory that all have to have the 
word "UNIX" changed to "Unix Operating System" in them?  Here's a 
simple way:

for file in *.html
do
    (echo 'g/UNIX/s/UNIX/Unix Operating System/g'; echo w; echo q) |
        ed $file
done

Done.  Note, some versions of ed(1) support chaining some commands 
together, and some support combining the 'w' and 'q' commands into a 
single 'wq', but the original ed does not.


There are no more items selected.

You have several choices: