「nl」對達爾文所犯的最少的驚訝

「nl」對達爾文所犯的最少的驚訝

越界最不驚訝

bash-5.0$ (for i in {1..1000010}; do echo $i; done) | nl | tail -24
999987  999987
999988  999988
999989  999989
999990  999990
999991  999991
999992  999992
999993  999993
999994  999994
999995  999995
999996  999996
999997  999997
999998  999998
999999  999999
000000  1000000
000001  1000001
000002  1000002
000003  1000003
000004  1000004
000005  1000005
000006  1000006
000007  1000007
000008  1000008
000009  1000009
000010  1000010
bash-5.0$ uname -a
Darwin llc.local 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar  4 22:28:40 PST 20
20; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64        
bash-5.0$ sw_vers 
ProductName:    Mac OS X
ProductVersion: 10.15.4
[...]
bash-5.0$ _

與預期比較:

admin@ip-w-x-y-z:~$ (for i in {1..1000010}; do echo $i; done) | nl | tail -24
999987  999987
999988  999988
999989  999989
999990  999990
999991  999991
999992  999992
999993  999993
999994  999994
999995  999995
999996  999996
999997  999997
999998  999998
999999  999999
1000000 1000000
1000001 1000001
1000002 1000002
1000003 1000003
1000004 1000004
1000005 1000005
1000006 1000006
1000007 1000007
1000008 1000008
1000009 1000009
1000010 1000010
admin@ip-w-x-y-z:~$ uname -a
Linux ip-w-x-y-z 4.9.0-9-amd64 #1 SMP Debian 4.9.168-1+deb9u4 (2019-07-19)
 x86_64 GNU/Linux
admin@ip-w-x-y-z:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 9.9 (stretch)
Release:    9.9
Codename:   stretch
admin@ip-w-x-y-z:~$ _

我很好奇你對此有何想法以及如何克服。謝謝。

答案1

預設寬度新增的數字nl是 6 nl由 POSIX 標準指定

nl如果數字長於寬度,macOS會截斷數字(並且手冊記錄了這一點),而 GNUnl則不會(但沒有記錄這一點)。標準文本似乎不允許這兩種行為。

若要為需要超過 6 位數字的行進行編號,請使用下列-w選項指定較大的寬度nl

$ (for i in {1..1000010}; do echo $i; done) | nl -w 7 | tail -24
 999987 999987
 999988 999988
 999989 999989
 999990 999990
 999991 999991
 999992 999992
 999993 999993
 999994 999994
 999995 999995
 999996 999996
 999997 999997
 999998 999998
 999999 999999
1000000 1000000
1000001 1000001
1000002 1000002
1000003 1000003
1000004 1000004
1000005 1000005
1000006 1000006
1000007 1000007
1000008 1000008
1000009 1000009
1000010 1000010

相關內容