How To Add Line Numbers To Text Files On Linux

add line numbers to text files

Ever wondered how to add line numbers to the standard output of text files on your Linux? This brief guide explains how you can add line numbers to a given text file. There are multiple ways of adding line numbers to a file. Here I have covered 6 different methods to do it. I will keep adding more methods if I come across any other methods in future. Better bookmark this guide and come back later to see if there are any additions.

Method 1 – Using nl command

This command is dedicated for adding line numbers to a file. It writes the given file to standard output, with line numbers added. I have a file named file.txt with the following contents.

$ cat file.txt 
This is line1
This is line2
This is line3

This is line5


This is line8

As you see in the above output, the file has 8 lines, with three empty lines. Let us add the line numbers.

To do so, run:

 $ nl file.txt
 1 This is line1
 2 This is line2
 3 This is line3
 
 4 This is line5
 
 
 5 This is line8

nl doesn’t take empty lines into account. It only added the numbers to non-blank lines. If you want to number all lines including the blank lines, run:

 $ nl -b a file.txt 
 1 This is line1
 2 This is line2
 3 This is line3
 4 
 5 This is line5
 6 
 7 
 8 This is line8

Also, you can add a symbol after the numbers. For example, to add dot (.) after the numbers, run:

 $ nl -s "." file.txt 
 1.This is line1
 2.This is line2
 3.This is line3
 
 4.This is line5
 
 
 5.This is line8

You may want to align the width of the output. To do so, use -w flag like below.

$ nl -w2 file.txt

Method 2 – Using cat command

The cat command is used to display the contents of a file. If you want to add numbers to the output of a file, use -n flag like below.

 $ cat -n file.txt 
 1 This is line1
 2 This is line2
 3 This is line3
 4 
 5 This is line5
 6 
 7 
 8 This is line8

Alternatively, you can pass the standard output to new file like below.

$ cat -n file.txt > newfile.txt

You may also want to get rid of the repeated empty lines.

 $ cat -s -n file.txt 
 1 This is line1
 2 This is line2
 3 This is line3
 4 
 5 This is line5
 6 
 7 This is line8

Method 3 – Using awk command

To add line numbers to the output of a file using awk command, run:

$ awk 'BEGIN{i=1} /.*/{printf "%d.% s\n",i,$0; i++}' file.txt 
1.This is line1
2.This is line2
3.This is line3
4.
5.This is line5
6.
7.
8.This is line8

As may noticed, i have assigned the starting number as 1 in the BEGIN parameter.  You can assign any other starting number of your choice, for example 5, as shown below.

$ awk 'BEGIN{i=5} /.*/{printf "%d.% s\n",i,$0; i++}' file.txt 
5.This is line1
6.This is line2
7.This is line3
8.
9.This is line5
10.
11.
12.This is line8

Use the following command if you don’t want to take the blank lines into account:

$ awk 'BEGIN{i=0} {if($0 !~ /^$/) {printf ("%d.%s \n",i,$0); i++} else { print $0} } ' file.txt

If you think the above commands are bit difficult to remember, use the following command instead.

$ awk '{ print FNR " " $0 }' file.txt 
1 This is line1
2 This is line2
3 This is line3
4 
5 This is line5
6 
7 
8 This is line8

If you want to increase the space between the numbers and the text, run:

$ awk '{ print FNR "\t " $0 }' file.txt

Method 4 – Using sed command

To add line numbers to a standard output of a file using sed command, run:

$ sed '/./=' file.txt | sed '/./N; s/\n/ /'
1 This is line1
2 This is line2
3 This is line3

5 This is line5


8 This is line8

The sed command has a cool feature that I like the most. We can display a Nth line from a file. For example, to display the 3rd line in a file, run:

$ sed -n 3p file.txt 
This is line3

Method 5 – Using less command

To line number to the standard output of a file using less command, run:

 $ less -N file.txt 
 1 This is line1
 2 This is line2
 3 This is line3
 4 
 5 This is line5
 6 
 7 
 8 This is line8

Method 6 – Using grep command

Grep command can be used to search for a line that contains a specific line. If you want to add the line numbers to a line that has a specific letter, for example line, run:

$ grep -n "line" file.txt 
1:This is line1
2:This is line2
3:This is line3
5:This is line5
8:This is line8

Please note that this command will only add the numbers to the lines that contains the search string. Everything else in the given file will be omitted.

And, that’s all. For more details of the above commands refer the man pages. You know now the different methods of adding line numbers to the text files. I hope you find this useful. More good stuffs to come. Stay tuned!

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *