A bit about using the ed line editor

I'm not sure what the difference is between the ex and the ed editors, but I think most of the stuff below is relevant to both. The O'Reilly Unix in a Nutshell book has a section on ex that seems to cover the same things I've found about ed.

Be sure to check the man page, especially the example at the end. The syntax is similar to that of sed, as shown in this example:

cat - << EOF | ed -s file-1
1,$ s/abc/xyz/
w file-2

In the above example, ed will scan all lines (1,$) and substitute xyq any place it finds abc (s/abc/xyz/). In this example (from the man page) all the commands are done in one long command. You can also achieve the same effect by running

ed file

and then entering the commands at the prompt (which isn't realy a prompt, the cursor is just in the leftmost column). Entering a number will take you to that line number in the file. Here are some other helpful commands:

l = list, or print, the current line. '1,5l' will list lines one through five.
p = print the current line; doesn't show $ for newline character
q = quit, don't save changes
w = write changes to file. '1.5w file' will write lines one through five to file
n = prints the current line number and line