You are not logged in.

#1 2022-01-24 15:29:40

MS1
Member
Registered: 2018-02-02
Posts: 95

how can I add comma separators to numbers in a cat command

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 136534

So pipe that into something to add the commas?

Offline

#2 2022-01-24 15:36:17

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,690

Re: how can I add comma separators to numbers in a cat command

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

#3 2022-01-24 15:46:37

Morn
Member
Registered: 2012-09-02
Posts: 886

Re: how can I add comma separators to numbers in a cat command

Stack Overflow suggests to pipe it into "numfmt --grouping":

cat /sys/devices/system/cpu/cpufreq/policy0/stats/time_in_state|numfmt --grouping --field=1,2

https://stackoverflow.com/questions/937 … -separator

Last edited by Morn (2022-01-24 16:23:47)

Offline

#4 2022-01-24 17:01:53

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,492
Website

Re: how can I add comma separators to numbers in a cat command

I'm tempted to call animal control to come collect all these useless cats! tongue

numfmt --grouping --field=1,2 < /sys/devices/system/cpu/cpufreq/policy0/stats/time_in_state

You might also like some padding:

numfmt --grouping --field=- --padding=10 < /sys/devices/system/cpu/cpufreq/policy0/stats/time_in_state

Last edited by Trilby (2022-01-24 17:11:10)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2022-01-24 18:59:34

MS1
Member
Registered: 2018-02-02
Posts: 95

Re: how can I add comma separators to numbers in a cat command

Thank you community. I would like to learn how awk or sed could do it too if @ewaller wants to try it.

Offline

#6 2022-01-24 19:09:48

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 9,003
Website

Re: how can I add comma separators to numbers in a cat command


Jin, Jîyan, Azadî

Offline

#7 2022-01-24 19:13:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,492
Website

Re: how can I add comma separators to numbers in a cat command

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/whatever

This 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/whatever

EDIT: 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

#8 2022-01-24 19:21:43

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,732
Website

Re: how can I add comma separators to numbers in a cat command

https://shallowsky.com/blog/linux/cmdli … ommas.html

Trilby wrote:

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

#9 2022-01-25 13:01:39

Morn
Member
Registered: 2012-09-02
Posts: 886

Re: how can I add comma separators to numbers in a cat command

Trilby wrote:

I'm tempted to call animal control to come collect all these useless cats! tongue

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

Board footer

Powered by FluxBB