Awk, by Sed and Grep, out of Ed

You can trace the lineage of awk to sed and grep, and through those two programs to ed, the original UNIX line editor.

Have you ever used a line editor? If so, it will be much easier for you to understand the line orientation of sed and awk. If you have used vi, a full-screen editor, then you are familiar with a number of commands that are derived from its underlying line editor, ex (which in turn is a superset of the features in ed).

Let’s look at some basic operations using the line editor ed. Don’t worry—this is an exercise intended to help you learn sed and awk, not an attempt to convince you of the wonders of line editors. The ed commands that are shown in this exercise are identical to the sed commands you’ll learn later on. Feel free to experiment with ed on your own to get a sense of how it works. (If you’re already familiar with ed, feel free to skip to the next section.)

To use a line editor, you work on one line at a time. It is important to know what line you are positioned at in the file. When you open a file using ed, it displays the number of characters in the file and positions you at the last line.

$ ed test
339

There is no prompt. If you enter a command that ed does not understand, it prints a question mark as an error message. You can enter the print command, p, to display the current line.

p
label on the first box.

By default, a command affects only the current line. To make an edit, you move to the line that you want to edit and ...

Get sed & awk, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.