You are not logged in.
Is there a utility that will automatically add comma separators to numbers? Like 123456 becomes 123,456 to make it more human readable? My example is this
# cat /sys/devices/system/cpu/cpufreq/policy0/stats/time_in_state
600000 404
700000 8
800000 735665
900000 9
1000000 20
1100000 14
1200000 53
1300000 76
1400000 52
1500000 45
1600000 41
1700000 50
1800000 136534So pipe that into something to add the commas?
Offline
Looks like a opportunity for awk or sed. sed scripts always kick my butt, but it should be a one liner for awk.
You might look into these as an exercise. If you need help, I might have some time later to cobble something together.
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way
Offline
Stack Overflow suggests to pipe it into "numfmt --grouping":
cat /sys/devices/system/cpu/cpufreq/policy0/stats/time_in_state|numfmt --grouping --field=1,2https://stackoverflow.com/questions/937 … -separator
Last edited by Morn (2022-01-24 16:23:47)
Offline
I'm tempted to call animal control to come collect all these useless cats! ![]()
numfmt --grouping --field=1,2 < /sys/devices/system/cpu/cpufreq/policy0/stats/time_in_stateYou might also like some padding:
numfmt --grouping --field=- --padding=10 < /sys/devices/system/cpu/cpufreq/policy0/stats/time_in_stateLast edited by Trilby (2022-01-24 17:11:10)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Thank you community. I would like to learn how awk or sed could do it too if @ewaller wants to try it.
Offline
Jin, Jîyan, Azadî
Offline
Those are really the wrong tools for the job if you ask me. If you can safely assume no numbers are over 1 million, then sed can easily insert the thousands separator:
sed 's/\([0-9]*\)\([0-9]\{3\}\)\w/\1,\2/g' < /path/to/whateverThis could be expanded further to also insert any necessary millions separators ... and then again for billions, etc. You could make it a bit more general purpose for any size numbers by looping:
sed ':a;s/\([0-9]*\)\([0-9]\{3\}\)\w/\1,\2/;ta;' < /path/to/whateverEDIT: the printf one from above is nice if you use bash and / or coreutil's printf:
/bin/printf "%'d %'d\n" $(cat /path/to/whatever)Last edited by Trilby (2022-01-24 19:20:11)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
https://shallowsky.com/blog/linux/cmdli … ommas.html
You might also like some padding:
numfmt --grouping --field=- --padding=10 < /sys/devices/system/cpu/cpufreq/policy0/stats/time_in_state
Really nice!
Last edited by graysky (2022-01-24 22:11:07)
Offline
I'm tempted to call animal control to come collect all these useless cats!
numfmt --grouping --field=1,2 < /sys/devices/system/cpu/cpufreq/policy0/stats/time_in_state
In general I don't like to use this syntax, especially when talking to Linux newbies: It's far too easy to accidentally type ">" instead of "<" and overwrite your precious input file! Cannot happen with "cat something | "…
Offline