Shell Programming Blog Shell Programming Blog

Monday 24 September 2012

How to print the lines to the screen with line numbers

Unknown | 10:45 |


As you know, the CAT command prints the lines of the file to the screen in the ASCII format. This is one of the command that is used to view the content of the file without opening the file in an editor. Suppose if you want the lines of the files to be displayed on the screen with line numbers, you can use the below options.



1) CAT command:
From man page of CAT(1):
       -n, --number
              number all output lines

So,
$ cat -n file.txt | less

will print the line number in-front of each line.

2) LESS command: 
The CAT command displays all the content in a single go. But the LESS command displays the content of the file page by page. You can use the below options to use view the content of the file using LESS command.

Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading.

From man page of LESS(1):
       -N or --LINE-NUMBERS
              Causes a line number to be displayed at the beginning of each line in the display.

So,
$ less -N file.txt

will display the lines along with the line numbers.

3) you can set the less command to print the lines with line numbers by default using the below command.

$ export LESS='-RS#3NM~g'

0 Comments:

   

Post a Comment

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
3. Please do not spam, spam comments will be deleted immediately upon my review.

Regards,
Mohamed Abubakar Sittik A

 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming