nl: Line numbering, in a variety of ways

nl, out of coreutils again, looks like a fairly simple program.

2014-09-26-6m47421-nl-01

Given the proper flags though, nl suddenly becomes a wild and crazy guy.

2014-09-26-6m47421-nl-02

Like a lot of things hiding in coreutils, what looks simple has a lot of very cool options. And it’s clear that nl is capable of handling much more complex arrangements. Watch for options for headers and footers, logical pages and so forth. Most of those things … I don’t even know what they are.😐

I can attest to nl‘s usefulness; I’ve used it in the past to add sequence numbers to lists, and to number lines from (of all places) within vim.

And considering the acrobatics it might take to do the same thing without nl, it is definitely a keeper.

3 thoughts on “nl: Line numbering, in a variety of ways

  1. Pingback: Links 26/9/2014: LibreOffice Celebrations, Betas of *buntu | Techrights

  2. trophyninjashrub

    I don’t know about acrobatics. If you don’t have blank lines in what you’re numbering (or you were planning on doing nl -ba) you can cat -n
    If you do have blank lines and you don’t mind ditching them perhaps grep -n .
    If you’re in vim and you don’t want to permanently alter the file – just want to see the line numbers – :set nu
    I’m sure some masochist would explain how to do all this in awk.

    But those are all more like workarounds. When it comes to numbering lines specifically this seems like the right tool for the job. Thanks for bringing attention to it.

    1. K.Mandla Post author

      When I wrote that, I was thinking of how to manually add lines to an output, probably using something like head and tail, and adding a counter to the front, and managing all the formatting that would be required. You’re right, there are other tools even just in coreutils or util-linux that can do the job, and if I was forbidden to use nl, I’d probably rely on one of those. The attraction here for me is just how smoothly it can zero-pad, trim itself to three characters, insert a custom separator, skip blank lines, and so forth.πŸ™‚

Comments are closed.