nl

2015-09-03


将显示的内容添上行号
有时候需要查找特定行的时候挺有用的

用法如下

  • -b, 指定行號指定的方式,主要有兩種:
    -b a, 表示不論是否為空行,也同樣列出行號(類似 cat -n);
    -b t, 如果有空行,空的那一行不要列出行號(預設值);

  • -n, 列出行號表示的方法,主要有三種:
    -n ln, 行號在螢幕的最左方顯示;
    -n rn, 行號在自己欄位的最右方顯示,且不加 0 ;
    -n rz, 行號在自己欄位的最右方顯示,且加 0 ;

  • -w, 行號欄位的佔用的字元數。

参考自:http://linux.vbird.org/linux_basic/0220filemanager.php#nl

例如有如下一个文件 tower-of-hanoi.rkt, 内容如下

#lang racket

(define (tower-of-hanoi n a b c)
  (cond [(> n 0)
         (tower-of-hanoi (- n 1) a c b)
         (printf "~a ~a ~a~n" a '-> b)
         (tower-of-hanoi (- n 1) c b a)]))

(tower-of-hanoi 5 'a 'b 'c)

以下是该命令运行的一些结果

  • $ nl tower-of-hanoi.rkt

         1  #lang racket
    
         2  (define (tower-of-hanoi n a b c)
         3    (cond [(> n 0)
         4           (tower-of-hanoi (- n 1) a c b)
         5           (printf "~a ~a ~a~n" a '-> b)
         6           (tower-of-hanoi (- n 1) c b a)]))
    
         7  (tower-of-hanoi 5 'a 'b 'c)
    
  • $ nl -b a tower-of-hanoi.rkt

         1  #lang racket
         2
         3  (define (tower-of-hanoi n a b c)
         4    (cond [(> n 0)
         5           (tower-of-hanoi (- n 1) a c b)
         6           (printf "~a ~a ~a~n" a '-> b)
         7           (tower-of-hanoi (- n 1) c b a)]))
         8
         9  (tower-of-hanoi 5 'a 'b 'c)
    
  • nl -b a -n rz -w 9 tower-of-hanoi.rkt

    000000001   #lang racket
    000000002
    000000003   (define (tower-of-hanoi n a b c)
    000000004     (cond [(> n 0)
    000000005            (tower-of-hanoi (- n 1) a c b)
    000000006            (printf "~a ~a ~a~n" a '-> b)
    000000007            (tower-of-hanoi (- n 1) c b a)]))
    000000008
    000000009   (tower-of-hanoi 5 'a 'b 'c)