from ed and grep to sed and awk
-------------------------------------------------------
-------------------------------------------------------
The unix utilities sed and awk are both based on pattern matching, i.e. regular expressions.
Historically, ed was the first utility on Unix to use regular expressions. It was a line editor and so to make edits, we had to specify both the line address (using the line number or any set of words in that line) and the action to be performed (typically print the line to screen, delete the line or substitute a set of words on that line.)
Almost every shell utility in Unix has the impression of ed. That is, they have the features of both a programming language and of a text editor.
In ed -
* Every command is of the type '/pattern/ {action}'
* A pattern is always a regular expression.
* An actions are typically print, delete or substitute.
* If a pattern is not specified, then the action is performed on the current line.
For example,
/pattern/d # deletes the next line containing the regular expression "pattern"
g/pattern/d # deletes all lines containing the regular expression "pattern"
p # prints current line to screen
In fact, the name of the grep command is derived from the frequently used ed command g/re/p, which prints all lines with the regular expression "re."
In sed and awk, both the input file/stream and the command file/stream typically containg multiple lines.
The first line of the input is read in and all the commands are run against that line. As with ed, if a pattern is specified, then sed/awk will run the action only if the line matches the pattern. If no pattern is specified, then the action is invariably run.
This is what happens with each line of input till we reach end-of-file for the input file/stream.
References -
A Tutorial Introduction to the UNIX Text Editor
by Kernighan
Unix Seventh Edition Manual. (Volume 2A has the tutorial)
http://cm.bell-labs.com/7thEdMan/bswv7.html
sed & awk, 2nd Edition
Dale Dougherty
Arnold Robbins
Historical references -
awk - A Pattern Scanning and Processing Language (September 1, 1978)
SED - A Non-Interactive Text Editor (August 15, 1978) by Lee E. McMahon.