ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to find a way to convert large numbers into comma separated numbers using bash. I.e. 12345 -> 12,345 I tried using the printf solution: printf "%'d\n" 12345678 but it doesn't seem to work in Cygwin (probably because locale isn't supported yet). Is there a way to do this within bash?
I am trying to find a way to convert large numbers into comma separated numbers using bash. I.e. 12345 -> 12,345 I tried using the printf solution: printf "%'d\n" 12345678 but it doesn't seem to work in Cygwin (probably because locale isn't supported yet). Is there a way to do this within bash?
if you have Python and can use it, here's an alternative
Oh I just understood! In most of the world (or so I've read), and at least where I live, the comma stands for the decimal separator. I suppose “your” comma is a thousands separator, isn't it?
In the English speaking world your 2nd sentence is the case
PS re OP: That does work in bash:
printf "%'d\n" 12345678
12,345,678
Unfortunately that relies on locale which isn't available in Cygwin, although it works fine on a normal box. I'm just looking to format the output of any number, no decimals among then, to include comma's for presentation purposes. There are thousands of numbers so I cannot do them by hand. Here's an example:
The above did not work on my PowerMac running Tiger 10.4.11,
So I spent a few minutes to find the solution below. Since my Mac only allows Basic Regular Expression (BRE), I had to change a few things and add a few backslashes to escape the control characters:
Code:
echo "1234567890" | \
sed -e ': L
s/\([0-9]\{1,19\}\)\([0-9]\{3\}\)/\1,\2/
t L'
Note that the backslash on the first line must immediately precede a newline
and the limit of 19 for the first RE can be changed to the maximum number of
digits you may encounter. I haven't tried this on ubuntu yet but it should
work for any system that accepts BRE's (and has sed).
To see how the command works, remove the branch (t) command. You'll see that
the first iteration puts the last (rightmost) comma in. Each successive
loop matches the left portion of the number without commas. The last loop
exits because there's nothing left to match.
Last edited by turtlegeek; 02-27-2011 at 04:12 PM.
Thanks for the translation to "BRE". If nothing else, you show why it is important to some of us to seek out a ver. of sed that has the "-r" option available. (Thanks, Reuti.)
Technically, it's not that your Mac doesn't have "extended regular expressions", it's the ver. of sed which it supplies.
Please look into & use [code] blocks. BTW, if you like, you can edit your post to add them.
Good use of the backslash & separate lines. It's clearer that way.
I re-ran & re-tested my orig. code & I find that the spaces in the input string & the '\b'-s used to find & clear them were unnecessary.
In GNU sed the "-e" is unnecessary.
Unless your ver. of sed doesn't allow it, your sed expression would be clearer if you used "s=...=...=", instead of "s/.../.../". -- Given the number of backslashes necessary to convert to BRE, it becomes a "kung fu regex"
I assume that your sed doesn't allow "[0-9]+", so you used [0-9]{1,19}. BTW, it could have been "[0-9]{4,19}".
My "g" option is unnecessary.
Good point about the backslash on the first line.
Great explanation of how it works.
Thanks for bringing this back to my attention, I had almost as much fun reviewing & rethinking it as I did originally.
Reuti,
Thanks for your suggestion.
My tested updated ver.:
Code:
echo "1234567890" | \
sed -r '
:L
s=([0-9]+)([0-9]{3})=\1,\2=
t L'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.