Navigation

  • index
  • previous |
  • Introduction to ed »

Related Programs¶

There are also a couple of programs where the understanding is related to that of ed.

grep¶

Searching is such a widespread operation that there is a tool dedicated for this, called grep, as it is just g/re/p. Note, however, that there is a subtle difference: there is no need for grep to keep the whole file in memory.

diff¶

diff compares files line by line. It can also output the difference in various formats. One of those formats is as ed script. Remember that ed takes commands from stdin which need not be interactive. The option is -e.

Here is an example. Note that the insertion of a line containing a single dot is done correctly.

$ cat foo
This is some text.
Here is still the first paragraph.

Here comes the second paragaph.
It has more text.

And, finally there is a third paragraph
which will not be present in the modified
file.
$ cat bar
This is some text.
Here is still the first paragraph.

This paragraph isnewly added. It also
contains a line with single dot
.
above this one.

Here comes the second paragaph.
It has more text.
$ diff -e foo bar
6,9d
3a
This paragraph isnewly added. It also
contains a line with single dot
..
.
s/.//
a
above this one.

.
$

Exercise. Why are the changes listed from the end to the beginning of the file?

rcs¶

Ending input with a single-dot line is convenient for interactive editing. For edit sequences generated by a program, however, the need to quote is additional work and makes these instructions slightly harder to read and to parse. So, there is another format for edit diffs, called “rcs diffs”. Instead of ending a block of text with a speical line, we mention ahead of time the number of lines that follow. Also, changes are presented in so-called “forward order”, i.e., from the beginning of the file towards the end. Note that this is in reverse order as the diffs have to be applied.

With the files as in the example above we get.

$ diff -n foo bar
a3 5
This paragraph isnewly added. It also
contains a line with single dot
.
above this one.

d6 4
$

Exercise. Write a program that transforms rcs diffs into ed diffs.

Storing instructions on how to edit one file to obtain another one is a very condensed way of storing two similar files. Similar files occur if we store the various versions of a file. As the version history of a file is organised as a tree, it is enough to store one version in full and diffs along the edges. To avoid overhead, it makes sense to store the version used the most often in full, i.e., the head revision.

Exercise. Read the rcsfile(5) man page to see how rcs implements this idea. Write an rcsfile by hand from scratch.

Exercise. Read co(1) and ci(1) to see how to use rcs.

Table Of Contents

  • Finite Automata
  • Regular Expressions
  • Basic ed concepts
  • Interaction with other programs
  • Related Programs
    • grep
    • diff
    • rcs

Navigation

  • index
  • previous |
  • Introduction to ed »