Friday, October 28, 2011

Print line number with unix less command


Less command introduction:
As you know Unix less command writes the contents of a file onto the screen a page at a time and this is one of the utilities using which one can view the content of a file without opening it in an editor.
Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading.

This is how we can print line number with Unix less command.

1)
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)
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 do the same as 1) above.

3) One can set the following to print line number with less by default:
$ export LESS='-RS#3NM~g'




Related posts:
- Numbering lines in a file using Bash awk
- Open file at required line number in Unix vi editor

© Jadu Saikia www.UNIXCL.com