There is an incredible selection of little Linux commands that will do all sorts of things to your files, standard output and programs. Now and again I’m going to pick one out and have a play because you just never know when a little tool already exists that will do exactly what you need.
Today I’m going to look at nl, which prepends line numbers to any file that you pass it to.
Remember this one as “number lines” (think Numberwang on Peep Show). Don’t get confused and use ln, because that is for linking files 🙂
Okay, so I’ve got a little C program, and you can view the source code on the command line with cat:
Very nice. Now let’s output the file with nl instead:
Look at that! Numbers down the side. Isn’t that handy?
But that’s not all.
Say you wanted to number all the lines – not just the ones with source code. This time use the flag -ba:
nl -ba main.c
Now let’s go a little retro, start the numbering at 10 and increase it by 10 each time (BBC Basic anyone?).
Use -v to start at your required number and -i to specify the increment:
nl -ba -v10 -i10 main.c
You can even add a string in front of each line.
Here I’ve added a colon after each number:
nl -ba -v10 -i10 -s": " main.c
The only thing missing really is the option to add disco colours and flashing text 😉
Check out the man page for full details on all the options (man nl).