Linux provides many text formatting demand line tools, with every of these having a purpose that is different. One utility that is such nl, which enables you to total lines in files. Within guide, we shall discuss the basics of this command using some easy to understand examples.
But before we do that, it’s worth mentioning that all examples used in this tutorial have been tested on an Ubuntu 16.04 LTS system.
Linux nl command
As already mentioned in the beginning, the command that is nl lines in files. After is its syntax:
nl [OPTION]… [FILE]…
And listed here is the way the guy web page describes it**********) that is:(**************)Write each FILE to standard output, with line numbers added. With no
FILE, or when FILE is -, read standard input.
Following are some Q&A styled examples that should give you a better idea on how works that are nl
Q1. Utilizing command that is nl
Basic usage of nl is very easy – all you have to do is to pass as argument the name of the file whose lines you want to number.
Q2. How to number empty lines?
By default, the nl command doesn’t number empty lines:
However, you can change this behavior by passing value ‘a’ to the -b command line option.( if you want,**********)
nl -b a [filename]
Q3. Just how to personalize the true number increment value?
By default, the number increment value is 1. However, you can customize this using the command that is-i choice.
nl -i [new-inc-val] [filename]
Q4. Making consider that is nl empty lines as one?
For this, use the command line option that is-l. Listed here is the way the guy web page describes it:
think about QUANTITY (standard 1) consecutive empty lines become one( line that is***************)logical numbering, and only number the last one. Where fewer than NUMBER consecutive lines that are empty, usually do not quantity them. An line that is empty one that contains no characters, not even spaces or tabs.
For example, let’s take the file that is following**********)
And assume, we would like nl to think about 8 consecutive empty lines become one rational line for numbering. Then listed here is the demand we would run:
nl -b a -l 8 [filename]
Q5. Utilizing numbering that is different?
You can use the -n command line option to specify numbering formats. Available option include left justified, right justified, and right justified with leading zeros. You can access these using the ln, rn, and rz values.
Q6.How to use a numbering that is customized?
By standard, the type that separates quantity through the line is a TAB. But you can customize this using the -s command line option, which requires the new separator as input.( if you want,**********)
For example, then here’s how we can do that:
nl -s :: [filename]
Q7 if we want to use a double colon (::) as the new separator. Making use that is nl different starting line number?
By default, numbering starts with 1. However, this you can change using the command line option that is-v. Listed here is just how:
nl -v [new-start-number] [filename]
The nl demand is not hard to know and make use of. Right here, within guide, we have covered most of the demand line choices it provides. An individual will be done exercising these, check out the device’s guy web page for more information.