What is sed?

Some people, when confronted with a problem, think “I know, I’ll use sed.” Now they have two problems.
– Jamie Zawinski, in The UNIX-HATERS Handbook

One of those long forgotten (by many) tools is sed, short for stream editor, a utility that parses and transforms text. It is a simple and compact programming language, developed from 1973 to 1974 by Lee E. McMahon of Bell Labs. It is one of a number of tools, like awk, tr, and now perl, which can be used to manipulate text. It is considered somewhat antiquated now, but still worth looking at. People tend to shy away from it because it is cryptic. Here’s a simple example:

sed '/pattern/d' file

This will delete all lines in which the pattern exists, and print the results to the standard output. So if the pattern is circle, and the input (in file vader.txt) is

I’ve been waiting for you Obi-Wan. We meet again, at last.
The circle is now complete; when I left you, I was but the
learner, now I am the master. Only a master of evil, Darth.

the output will be:

I’ve been waiting for you Obi-Wan. We meet again, at last.
learner, now I am the master. Only a master of evil, Darth.

Here is a second example which strips a C program of inline comments using // (anywhere they occur).

sed 's://.*$::g' comments.c

The s is the substitution command, more commonly seen as s/…/…/, but as in the case above using a colon as the delimiter, i.e. s:…:…:. The // implies the string to search for. The .* matches zero or more characters after the first match. The $ appended on specifies the whole line. The g implies global replacement. So given this input (in comments.c):

#include <stdio.h>

int main(void)
{
    // Declare the variables for input
    double a,b;

    // Read in the values for the two numbers
    scanf("%lf%lf", &a, &b);
    // Compare the numbers
    if (a == b)
       printf("The numbers are equal\n"); // If equal
    else
       printf("The numbers are not equal\n"); // If not equal

    return 0;
}

The output would be:

#include <stdio.h>

int main(void)
{

    double a,b;


    scanf("%lf%lf", &a, &b);

    if (a == b)
        printf("The numbers are equal\n");
    else
        printf("The numbers are not equal\n");

    return 0;
}

Nice? This is essentially what the C compiler does before it starts parsing the code.

Go on… teach yourself sed!

A short guide to sed can be found here.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s