Sometimes you could have the need of numbering the lines of a text file. If the text file is very short you can do this job by hand, but if the number of lines of the text files is high this is a tedious job.

Almost all of Unix flavors provides the nl tools.

from the man page:


numbering tool

Imagine you have a ruby source file, and you want to number the lines of the file, if your file is the following:

you have to issue the following command:

now let's see the output:

now let's see the output:


for some strange reasons you could want to number lines with different interval, try the following command:

now imagine you want to separate line numbering and file contents with your preferred separator, it could be useful to output as .csv

The nl utility treats the text it reads in terms of logical pages, so you can also restart numbering at the end of your page. Take a look at the man page.