+3 votes
268 views
How to number the lines of a file in the Linux console

in Guides by (552k points)
reopened | 268 views

1 Answer

+4 votes
Best answer

How to number the lines of a file from the Linux console with the nl command.
Number the blank lines followed as a single line.
Number the lines with an incremental value.
Alignment of the numbering.
How to start numbering by a certain line number.

Linux always offers us tools for more unexpected features, but sooner or later its need arises. In this case we are going to talk about the tool or command nl with which we can number the lines of a file from the Linux console .

image

Although this functionality seems irrelevant, when we work from the console or terminal, we need to use this tool. I have personally used it to work with text files, CSV files, database dumps , configuration files, etc.

I must also recognize that I am in love with the nano editor and this by default does not show on the screen the numbering of the lines, so sometimes I have had to use the nl command.

How to number the lines of a file from the Linux console with the nl command.

In this first section of the tutorial, we show the most basic use of the nl command, which would be the following:
nl TechnoWikis.txt

image

In the image we see that all the lines have been numbered except those that are blank. We might need to number all the lines, regardless of whether they are empty or not, for this case, the command would be:
nl -ba TechnoWikis.txt

image

So far we would have what we could call the tutorial of basic use of the nl command and that covers the most basic needs. Next, we will cover other less common situations that we can solve with this tool.

Number the blank lines followed as a single line.

The numbering of blank lines is something simple, but sometimes we will need that if there are several blank lines in a row, it will be counted as a single line . The nl command is an SUV and allows us to solve this problem by executing:
nl -ba -l 2 TechnoWikis.txt

We see that in this command we have used the -l 2 modifier, which is responsible for assigning a line number when it finds two blank lines. If the modifier were -l 5 , there would have to be 5 blank lines in a row for a line to be counted.

Number the lines with an incremental value.

This seems somewhat complex, in reality it is very simple, it consists in numbering the lines incrementally but with a jump value for each line. To understand it better, we are going to give an example that consists of executing the command:
nl -i 3 TechnoWikis.txt

image

As we see in the image, the lines have been numbered but assigning an incremental value of 3, that is, the first line corresponds to the value 1, the second line the value 4 (3 + 1) , to the third line the value 7 (4 + 3) and so on.

This functionality is used at very specific times, but it is good to know that this possibility exists.

Alignment of the numbering.

This command also allows us to play with the alignment of the numbering and its format. This can be very useful when we are going to paste the values "‹"‹obtained in Excel or office software.

With nl we can define the -n modifier that allows you to assign three types of numbering alignment :

  • Right alignment: ln
  • Left alignment: rn
  • Right alignment with leading zeros: rz

image

Within this section, we can also include the -s option that allows us to specify a custom separator for numbering . By default, the separator is a tabulation, but to modify it we can execute the following command: nl -s " -> " TechnoWikis.txt

image

How to start numbering by a certain line number.

On many occasions we will need the numbering of the lines to start with a value other than 1. Personally I have encountered many situations in which I need to start numbering from the zero value. To achieve this, we have at our disposal the -v modifier that allows us to set the initial line numbering value .

An example of use would be: nl -v 0 TechnoWikis.txt

image

We can appreciate that the nl command is very flexible and offers us infinite possibilities. Even so, we can find situations that have not been included in this tutorial, so it is advisable to take a look at the help page with the man nl command.

If you have any questions or just need more information, you can use the comments or the forum.


by (3.5m points)

Related questions

+5 votes
1 answer
+3 votes
1 answer
+5 votes
1 answer
+5 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (552k points) | 214 views
+3 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (552k points) | 253 views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users