Sam Newman's site, a Consultant at ThoughtWorks

I was about to post some helpful tips on using @sed@ to generate code, but thought a brief introduction of @sed@ was in order.

The history of qed, ed and sed

@sed@ is the stream version of the @ed@ command line editor (itself an offshoot of Ken Thompson’s “qed(An incomplete history of the QED Text Editor by Dennis Ritchie)”: Put simply, @ed@ was developed in a time when the capabilities of computer terminals fall far short of what is possible today – back then an editor like @vi@ wasn’t even possible. You couldn’t navigate to the piece of text you wanted to change, delete and then replace it – instead @ed@ presented with a view of your text and a command line. To change your text, you had to enter a command and watch the text change – a method which some people likened to a virtual typewriter (whereas the later @vi@ editor provided “virtual paper”). For example to replace the first occurrence of @bob@ with @fred@ you’d type the following:


@ed@ also supported regular expressions (albeit a simplified set of the regular expression support available in @qed@) – you could for example run a command to print out all the text that matches a regular expressions. From Jeffrey E. F. Friedl’s Mastering Regular Expressions:

The command “g/Regular Expression/p”, was read “Global Regular Expression Print”. This particular function was so useful that it was made into its own utility, @grep@ (after which @egrep@ – extended @grep@ was later modelled).

Introducing sed

@sed@ came from the need to run a series of @ed@ commands on a stream, without having to edit each file. commands could now be run on the command line like this:

sed g/Regular Expression/p file

Or could be put into a file of commands so several could be run at once:

sed -f sedcmdfile file

Being run on the command line, you could run sed on several files:

sed -f sedcmdfile *.txt

Or even on a whole directory tree – for example this finds all files with a @java@ suffix and executes sed on them:

find . -name "*.java" -print | xargs sed -f sedcmdfile {}

Its worth noting however that @sed@ simply outputs to @stdout@, so you’ll need to redirect the result into a file – as such running sed is of limited use unless you run it within a wrapper script to edit the original file, I’ll dig out a decent example and post it later. Next up, I’ll post a helpful tip or two to get you started with @sed@.

Getting sed

Sed has been a standard part of Linux and Unix distributions for years now, and has been ported to many systems. On windows, it is shipped as part of “cygwin”:

Further reading

  • “Mastering Regular Expressions, 2nd Editing by Jeffrey E. F. Friedl”: This covers regular expressions as a whole, looking at various implementations in grep, Perl, Java and .net
  • “Sed & Awk,2nd Edition by Dale Dougherty, Arnold Robbins”: this was my bible when I started learning sed, and remains the definitive text on the subject.
  • “Communications of the ACM – Regular expression search algorithm(The ACM Digital Library – Programming Techniques – Regular expression search algorithm)”: This is Ken Thompson’s original paper on Regular Expressions, the work for which was introduced into @qed@. Registration is required for the full (pdf) paper.

One Response to “A brief history of ed, sed and Regular Expressions”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS

%d bloggers like this: