Submind YouTube summaries
Thumbnail for 'nl' - number lines of files - Video Man Pages

'nl' - number lines of files - Video Man Pages

Watch on YouTube

Video summary

The `nl` command is a versatile GNU core utility designed to add line numbers to files, making it easier to reference specific lines within scripts or text documents. By default, when run without any flags, the tool numbers only non-empty lines, skipping blank lines entirely. This behavior allows users to quickly view the structure of configuration files like `.bashrc` while ignoring whitespace-only gaps. Beyond processing files directly from the command line, `nl` can also accept input via pipes, enabling users to add line numbers to the output of other commands in real-time, such as numbering a simple sequence generated by the `seq` command. To gain more control over how lines are numbered and displayed, the utility offers several specific flags that alter its default behavior. Users can force the numbering of empty lines using the `-b` flag combined with options like `a` for all lines or `n` to suppress line numbers entirely while still reserving space for them. Additionally, the command supports custom increment values via the `-i` option, allowing line numbers to increase by a specific interval rather than sequentially by one. This feature is particularly useful for creating non-standard numbering sequences, although it may result in a higher total number of lines in the output depending on how the increments align with the file's content. Further customization is available through options that adjust the visual formatting and layout of the numbered output. The `-w` flag allows users to define the width of the reserved space for line numbers, preventing excessive whitespace or optimizing space usage based on the expected number of digits. Furthermore, the `-n` option provides various number formats, such as right-justified with leading zeros, right-justified without zeros, or left-justified, giving users flexibility to match specific formatting requirements. Finally, the `-s` flag lets users replace the default tab separator between the line number and the content with any custom character or string, offering a clean way to integrate numbered lines into different contexts or data formats.
Read the full video transcript
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. 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 1 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 get. Lines 1 through 10, right? It gave me the line numbers and then the lines, which also contain 1 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 {dash} 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 number. 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 -bn 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 an increment for the line numbering. So, if you don't want the line numbers to go 1 2 3 4, maybe you want it to go 2 4 6 8 or 3 6 9 12. All right, 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 5 .bashrc, it's going to add line numbers to all the nine empty lines of my bashrc, except 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 uh 238 lines, if I don't want to waste space, I could specify -W3. And now, yeah, 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 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, uh 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 -S for separator. So, by default, when you uh do the NL command, the separator between the numbers and then the output of the lines is a you can specify a different separator. For example, maybe I want the uh 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, you know, 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.