The 'nl' command adds line numbers to files or to terminal output.
+ Number non-blank lines in a file: nl path/to/file
+ Number non-blank lines in ouptut from stdin: command | nl ex: seq 10 | nl
+ Number [a]ll [b]ody lines including blank lines or do [n]ot number body lines: nl -b a path/to/file
+ Do [n]ot number [b]ody lines (essentially this suppresses the line numbers): nl -b n path/to/file
NOTE The above command is not that different than 'cat' or 'less' except that 'nl -b n' does reserve space for where the line numbers would have been.
+ Number only the lines matching a basic regex [p]attern: nl -b p'FooBar[0-9]' path/to/file -OR- nl -b p'^#' .bashrc
+ Use a specific [i]ncrement for line numbering: nl -i NUMBER path/to/file
+ Specify the line numbering width (default = 6): nl -w NUMBER path/to/file
+ Specify [n]umber-format as [r]ight justified with leading [z]eroes: nl -n rz path/to/file + Specify [n]umber-format as [r]ight justified with [n]o leading zeroes: nl -n rn path/to/file
+ Specify [n]umber-format as [l]eft justified with [n]o leading zeroes: nl -n ln path/to/file
+ Use a specific string to separate line numbers from the lines (default = TAB): nl -s ' | ' path/to/file
Your support is very much appreciated. Thanks, guys!
AI Generated·May contain errors
Video Summary
AI Generated
This video provides a comprehensive tutorial on the GNU Core utility `nl`, which is used to add line numbers to files or piped command output. The instructor demonstrates various flags to control which lines are numbered, the numbering increments, spacing, alignment, and the separators used between the numbers and the text.
A really neat GNU Core utility is the nl command.
What does the nl command do?
It adds line numbers to files, right?
It numbers lines.
And let me jump into the terminal
and I'll show you exactly what the nl command does.
If I type nl, and let's do this on my .bashrc file,
and if I give it no flags or options,
by default it adds line numbers
to all the non-empty lines of that file.
So anything that's an empty line does not get a line number,
but every line that actually had characters,
that gets a line number.
And other than using nl and then a path to a file,
you could also pipe things into nl.
I could do command pipe nl to add line numbers
to the output of the first command.
For example, let me sequence 10.
And all that does,
it prints a sequence of numbers one through 10.
Now, if I up arrow and pipe that into nl
to add line numbers to that output,
what would you expect to get?
Well, exactly what I just got.
Lines one through 10, right?
It gave me the line numbers and then the lines,
which also contain one through 10,
because that's what was actually printed on those lines.
Now, earlier when I ran nl on my .bashrc,
I mentioned that it does not, by default,
add line numbers to empty lines.
If you want to add line numbers to the empty lines,
you need to add a couple of flags here.
You need to do nl-b for body lines.
I don't know why they call them body lines.
And then a for all the lines and then the path to a file.
And now I get line numbers on all the lines
of my .bashrc file.
One other thing you can do with the dash-b flag
is dash-b-n for no lines.
And that actually just suppresses the line numbers.
So it just prints out my .bashrc with no line numbers.
And you may think, well, what's the purpose of that?
I mean, isn't that the same as catting out your bashrc
or running the less command on .bashrc?
Well, it is very similar as far as the output itself.
You do notice that when you run the nl-b-n for no lines,
it does reserve space for the line numbers, right?
There's six empty spaces, which is the default spacing here.
It reserves those six empty spaces for the line number
had it been printed.
Sometimes you want to search for lines
based on a regex pattern.
If I up arrow, one thing you can do
is you can do nl-b for body lines.
And we're going to search for lines that contain a pattern.
So type p and then single quotes, two single quotes.
Inside the single quotes should be the pattern
that you're searching for.
It can be a regex pattern.
For example, maybe I want to search for the beginning
of a line that begins with the pound symbol
because I know my .bashrc is essentially a bash script.
Comments in bash begin with a pound symbol.
So any commented line will start with a pound symbol.
And now I get that as output
and I get the lines that contain that numbered.
One neat and kind of weird thing you can do
with the nl command is you can specify a increment
for the line numbering.
So if you don't want the line numbers to go one, two, three,
four, maybe you want it to go two, four, six, eight,
or three, six, nine, 12,
or you can specify increments.
Again, I'm not sure exactly why I would ever
want to do this, but for example,
if I wanted to do nl-i for increment five .bashrc,
it's going to add line numbers
to all the non-empty lines of my .bashrc.
And so it is going to do them in increments of five
because my .bashrc had 200 and something lines, right?
But now I have 1186.
Well, that's because the numbering system
is now incremented up every fifth number.
Earlier, I mentioned that there is six spaces reserved
for line numbering by default with nl.
You can change that.
I can do nl-w for width and reserve eight spaces,
for example, and now you can see that I have eight spaces
reserved for the line numbering here on my .bashrc.
If I only wanted, because I know there's only 238 lines,
if I don't want to waste space, I could specify dash w three
and now it gets rid of all the leading empty spaces, right?
Because it only reserves three spaces now
for the line numbers,
which is plenty for this particular file.
A couple of other flags you need to know about,
nl-n for number format,
and there's several different number formats you can use.
RZ is right justified with leading zeros.
So if I run that, you get right justified,
and in that case, we also get leading zeros.
If I up arrow, R is right justified, Z is the leading zeros.
If you don't want the leading zeros,
you could do R for right justified, N for no zeros,
and this looks more like your standard default nl command.
If I up arrow, I can also do ln for left justified,
no leading zeros, and there you go.
You can see no leading zeros,
and it does left justify everything.
So I don't have those blank spaces, those empty spaces
that are typically there in the default nl command.
One last flag I want to talk about is dash s for separator.
So by default, when you do the nl command,
the separator between the numbers
and then the output of the lines is a tab,
but you can specify a different separator.
For example, maybe I want the pipe symbol
as a separator here, and let's do this on my .bashrc file,
and now you get line numbers, and then instead of a tab,
and then the output of that line,
you have, in this case, the pipe symbol.
That looks pretty bad.
Maybe I would do space pipe space and run that,
and that looks good.
Of course, you could specify anything here.
If I needed to add a colon as a separator,
I could add that as well.
So there you have it, a little bit of what you can do
with the nl command.
If you want to learn more, type man nl in the terminal
to read the man page.