Advertisement

Terminal Tip: the joy of nl

Too often overlooked, nl provides a very useful shell command. As you can see here, when issued, it numbers the lines of a text file. But that's certainly not all that this little utility can do. My favorite feature allows you to count the occurrences of a regular expression. Use the -bp flag to tell nl to count all the lines that match that expression. For example:

% nl -bp"So.*e" sonnet.txt
Shall I compare thee to a summer's day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date:
1 Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature's changing course untrimm'd;
But thy eternal summer shall not fade
Nor lose possession of that fair thou owest;
Nor shall Death brag thou wander'st in his shade,
When in eternal lines to time thou growest:
2 So long as men can breathe or eyes can see,
3 So long lives this and this gives life to thee.
%