How to add line numbers in unix shell output
How to add line numbers in unix shell outputTo display line numbers easily on a shell output, pipe it to "nl" command - line number filter.
Quote:
NL(1) FreeBSD General Commands Manual NL(1)
NAME
nl -- line numbering filter
SYNOPSIS
nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]
[-n format] [-s sep] [-v startnum] [-w width] [file]
DESCRIPTION
The nl utility reads lines from the named file or the standard input if
the file argument is omitted, applies a configurable line numbering fil-
ter operation and writes the result to the standard output.
The nl utility treats the text it reads in terms of logical pages.
Unless specified otherwise, line numbering is reset at the start of each
logical page. A logical page consists of a header, a body and a footer
section; empty sections are valid. Different line numbering options are
independently available for header, body and footer sections.
The starts of logical page sections are signalled by input lines contain-
ing nothing but one of the following sequences of delimiter characters:
Line Start of
\:\:\: header
\:\: body
\: footer
If the input does not contain any logical page section signalling direc-
tives, the text being read is assumed to consist of a single logical page
body.
This can be done with awk also, but I find this easier:
Code:
# ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
# ls | nl
1 file1
2 file10
3 file2
4 file3
5 file4
6 file5
7 file6
8 file7
9 file8
10 file9