Bash:wc 以人类可读格式输出行数

Bash: output line count from wc in human readable format

这可能吗?以直接的方式 wc 我必须花费一些精力才能看到​​该文件包含超过 4000 万行:

$ wc -l 20150210.txt 
    45614736 20150210.txt

我四处搜索,numfmt 出现了,但这显然在 OSX 上不可用(在 brew 上也不可用) .那么在 OSX 上有没有一种简单的方法可以做到这一点?谢谢

如果你有 POSIX printf 你可以使用 %'d:

printf "%'d\n" $(wc -l < file )

来自man printf

'

For decimal conversion (i, d, u, f, F, g, G) the output is to be grouped with thousands' grouping characters if the locale information indicates any. Note that many versions of gcc(1) cannot parse this option and will issue a warning. SUSv2 does not include %'F

测试

$ seq 100000 > a
$ printf "%'d\n" $(wc -l <a )
100,000

另请注意 wc -l < file 获取没有文件名的数字的技巧。