Displaying Line Numbers

October 15th, 2006 aizatto Posted in nl | Hits: 23504 | No Comments »

Some of you may be familiar with `wc`, the command that counts the number of lines, words, and bytes to a file. Quite a handy little tool, but sometimes you want the line number to be included to a file.

Say you have this bunch of text, and its difficult to refer to as it doesn’t have line numbers! Sure you can fire up `vi` and do a `:set numbers`, but thats inconvenient. Your not going to do that ever time you want to read the file is it? (Yes you can config vi more, but still inconvenient)

Today I shall tell you about the powers of `nl`. A small two letter command, but don’t let its size scare you. `nl` accepts files as an input, and outputs to the screen for you to view.

For example run:

nl /etc/passwd

You’ll get an output with each line numbered. In my original scenario, what you could do next is then output it to a file for viewing later ala:

 nl /etc/passwd > ~/passwd

Enjoy, and don’t forget to read the manual for more additional tips about `nl`

Leave a Reply