Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: output line count from wc in human readable format

Tags:

bash

macos

wc

Is that possible? Doing wc the straight forward way I have to spend some mental energy to see that the file contains more than 40 million lines:

$ wc -l 20150210.txt 
    45614736 20150210.txt

I searched around and numfmt showed up, but that is evidently not available on OSX (nor on brew). So is there a simple way to do this on OSX? Thanks.

like image 316
luffe Avatar asked May 12 '15 14:05

luffe


1 Answers

If you have POSIX printf you can use the %'d:

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

From 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

Test

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

Note also the trick wc -l < file to get the number without the file name.

like image 93
fedorqui 'SO stop harming' Avatar answered Sep 28 '22 17:09

fedorqui 'SO stop harming'