Thursday, December 27, 2012

Prepending number of lines to standard output using nl

nl copies files to standard output, with lines added. It is very flexible as it can prepend numbers to non blank lines and even justify left or right.

Usage 1: To add lines to standard output
$ nl -ba /etc/hosts.allow 

     1  #
     2  # hosts.allow   This file describes the names of the hosts which are
     3  #               allowed to use the local INET services, as decided
     4  #               by the '/usr/sbin/tcpd' server.
     5  #
     6
where -ba => prepend numbers to all lines

Usage 2: To add lines to non-blank text
$ nl -bt /etc/hosts.allow
     
     1  #
     2  # hosts.allow   This file describes the names of the hosts which are
     3  #               allowed to use the local INET services, as decided
     4  #               by the '/usr/sbin/tcpd' server.
     5  #
where -bt => prepend lines to non-blank lines

Usage 3: Format the numbering to Left Justify
$ nl - bt -nln /etc/hosts.allow

1       #
2       # hosts.allow   This file describes the names of the hosts which are
3       #               allowed to use the local INET services, as decided
4       #               by the '/usr/sbin/tcpd' server.
5       #
where -bt => prepend lines to non-blank lines
-nln => format the number to be left-justify

Usage 4: Format the numbering to Right Justify
 $ nl -bt -nrn /etc/hosts.allow

     1  #
     2  # hosts.allow   This file describes the names of the hosts which are
     3  #               allowed to use the local INET services, as decided
     4  #               by the '/usr/sbin/tcpd' server.
     5  #

No comments: