Bash: output line count from wc in human readable format
Asked Answered
O

1

7

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.

Ocd answered 12/5, 2015 at 14:25 Comment(2)
Does printf "%'d\n" 45614736 do what you want on OS X? (I don't know how portable that format string is.)Ticonderoga
Note gnumfmt should be available throgh brew install coreutilsUria
S
9

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.

Sikko answered 12/5, 2015 at 14:28 Comment(4)
Thank you, but when I do $printf "%'d\n" 45614736 i get the output 45614736 with no formatting. Strange?Ocd
The output from %'d is locale-dependent. What is the output of locale | grep LC_NUMERIC?Stickpin
Ah, it says LC_NUMERIC="C"Ocd
I did LC_NUMERIC="en_US.UTF-8". Now it works. ThanksOcd

© 2022 - 2024 — McMap. All rights reserved.