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

5 comments:

jamerfort said...

While in less, you can press '-N' to turn line numbers on.

Gonzalez said...

Mr. expert,

Im clueless as how to work with simple date arithmetic. Im running on old bash(BSD Legacy).

if I give 2 inputs, 2010-12-28 and 2011-01-02 then I should see the following -
2010-12-28
2010-12-29
2010-12-30
2010-12-31
2011-01-01
2011-01-02


No matter what i do, the system prompts back with errors as below -

If you can help with this, Im thankful.
Gonzalez
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Gonzalez said...

Mr. expert,

Im clueless as how to work with simple date arithmetic. Im running on old bash(BSD Legacy).

if I give 2 inputs, 2010-12-28 and 2011-01-02 then I should see the following -
2010-12-28
2010-12-29
2010-12-30
2010-12-31
2011-01-01
2011-01-02


No matter what i do, the system prompts back with errors as below -

If you can help with this, Im thankful.
Gonzalez
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Unknown said...

@Gonzalez,

Thanks for your question:

I can think of something using

DATE(1) %s option [ seconds since 1970-01-01 00:00:00 UTC ]
with date string -d


$ date +%s -d'2010-12-28'
1293474600

$ date +%s -d'2011-01-02'
1293906600

$ seq 1293494400 86400 1293926400
1293494400
1293580800
1293667200
1293753600
1293840000
1293926400

$ seq 1293494400 86400 1293926400 | xargs -i date -d @{}
Tue Dec 28 05:30:00 IST 2010
Wed Dec 29 05:30:00 IST 2010
Thu Dec 30 05:30:00 IST 2010
Fri Dec 31 05:30:00 IST 2010
Sat Jan 1 05:30:00 IST 2011
Sun Jan 2 05:30:00 IST 2011


Let me see if we can perform this mathematically using a script.

Vitalie said...

or another alternative:

prmt#> ls -l | nl | less

Thank You :)

© Jadu Saikia www.UNIXCL.com