Search code examples
macosbashwc

Bash: output line count from wc in human readable format


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.


Solution

  • 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.