Tag Archives: count

nl: Line numbering, in a variety of ways

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


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


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.

seq: Count up, count down

A quick note before we get into seq today: If you left a comment in the past 18 hours or so but don’t see it by now, it might have gotten lost in a spam avalanche.

Someone out there in the world must have turned up the dial on the spam machine, because I had more than 120 spam comments between noon today and early yesterday evening. For reference, I usually see 10 or 12 in a day.

Usually WordPress.com — in spite of all my complaints — does a decent job of filtering out the winners from the losers, but sometimes people get lost in the fracas. If for some reason your comment got mixed in with the spam, I’m sorry, but it’s likely gone forever.

And just FYI: If you include a URL in your comment, it’s more likely to be flagged as dubious by the automated system. Sometimes I can pluck a comment out while it’s circling the drain, but this time there were too many to scan through. My apologies.

Anyways. … seq is up to bat today, and this won’t take long. seq doesn’t do much other than count, by increment, between a stop and a start number. Sounds simple enough.


The fun in seq is getting it to pad its output with leading zeros, which pleases people like me who want their numbered files to line up vertically down the screen. πŸ™„ And as you can see, you can set a delimiter for seq, which comes in handy at times.

It does pad things a little oddly at times — for example, that 00.0 in the screenshot above. And if you pad your numbers while counting in reverse (yes, you can do that), you’ll find that seq pads 0 as 00, and so forth. I should also mention that seq‘s last output doesn’t carry a separator. That means if you do something funky like this:

kmandla@6m47421: ~$ seq -w -s' '$(shuf -n1 /usr/share/dict/cracklib-small)$'\n' 1 1 10

You’ll see that every line but the last carries a random word. Just so you’re aware. πŸ˜‰ Things like that are a little unusual but not deal-breakers. Not like join was. 😐

That’s about all I need to say about seq. It comes in very handy at times, which is probably why it’s part of coreutils. πŸ˜‰

cloc: Clock your code

Line-counting utilities are not a new thing for K.Mandla; there have been a few of these available for a while. codemetre comes to mind, as does sloccount. Even wc could fit in that category.

Here’s cloc:


cloc has a couple of features I like. For one, cloc can work straightaway on a compressed file. You don’t need to extract an archive before cloc does its thing; cloc will handle the decompression automatically. And if it runs into trouble, you can assert your biological superiority and tell it which decompresser to use.

cloc also has some built-in diff tools, which might not sound like a wonderful idea at first. What that means though, is that it can look for differences between versions of programs, and report on those. This might be useful if you have an axe to grind with a developer. 😑

And cloc carries a lot of filtering options, on everything from regex strings to specific programming languages to hidden Windows files. That might have saved me a little time when counting lines in the 3.15.6 kernel, above. 😐

The sad part of this little exposΓ© is that I have so very, very little coding ability that cloc (or for that matter, codemetre or sloccount) isn’t something I’ll likely use. Unless, of course, I’m counting lines of text in a smarmy blog. ^^’

sloccount and sloc2html.py: Because size does matter

I’m not a coder, and I try whenever possible to repeat that, at the top of my lungs.

There are some coder’s tools that I think are nifty though. Way back in the C section we had codemetre, for checking the length and breadth of your efforts, and comparing that with the number of commented lines.

In a similar vein, here’s sloccount, which goes a step further by counting out lines of types of code, summing them across an entire project, and showing some basic analysis.


And that’s what you get if you point it at the source for the 3.14.2 kernel. πŸ˜‰

sloccount is nifty by itself, no doubt about it. If you’re willing to make a small leap toward the graphical, sloc2html.py will — as you might expect — convert the results of sloccount into something browser-oriented.


This is where things start to break down, because the home page for sloccount also hosts a touched-up version of sloc2html.py, but the link to the actual python program is dead. I scraped around the internets and I think I found a reasonable facsimile thereof, here. I have no way of knowing if that’s the right one though. πŸ˜•

The problem is that, as with so many python programs I encounter, it seems that advances in python proper spawn errors in sloc2html.py, the least of which seemed to occur with Arch’s python2 in charge. What you see above is what sloc2html.py could write out, before it crashed and burned.

I think you get an idea of what it should be doing though, from the screenshot. If you are a pythonian, you might be able to get it going with version 3+, with a little effort. I lack the requisite skills for that.

Which is what I mentioned at the start of this post. Clever, aren’t I? πŸ˜‰