Automatic line numbering

Ever wanted to include a piece of code somewhere, and also have line numbers in order to have something to refer to when you’re discussing the code.

Use nl.

It’s another of those totally useful Unix utilities – short for line numbering filter. And it’s simple to use. Used as nl test.c, it just numbers all non-empty lines, however if all lines should be numbered,  the parameters can be modified to nl -ba test.c. This takes a file like this:

#include <stdio.h>

int main(void)
{
    int a, b;
    double d;

    if (a == b)
        d = a * b;
    return 0;
}

And turns it into this:

     1  #include <stdio.h>
     2
     3  int main(void)
     4  {
     5      int a, b;
     6      double d;
     7
     8      if (a == b)
     9          d = a * b;
    10      return 0;
    11  }

 

 

 

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