Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
09-24-2008, 06:11 AM
|
#1
|
|
LQ Newbie
Registered: Jul 2007
Posts: 7
Rep:
|
Format numbers using 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?
Last edited by KClaisse; 09-24-2008 at 06:17 AM.
|
|
|
|
09-24-2008, 06:50 AM
|
#2
|
|
Member
Registered: Mar 2008
Location: Malta
Distribution: Slackware 14.0
Posts: 435
Rep:
|
num=12345
echo "${num%???},${num#??}"
|
|
|
|
09-24-2008, 07:22 AM
|
#3
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by ChrisAbela
num=12345
echo "${num%???},${num#??}"
|
this is not enough. num may not be 12345 everytime.
|
|
|
|
09-24-2008, 07:25 AM
|
#4
|
|
Senior Member
Registered: Jul 2004
Location: Nantes (France)
Distribution: Arch Linux
Posts: 1,897
Rep:
|
What exactly are you trying to achieve? How should 12345678 be rendered?
Yves.
|
|
|
|
09-24-2008, 07:26 AM
|
#5
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by KClaisse
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
Code:
#!/usr/bin/env python
import locale
locale.setlocale(locale.LC_ALL,('en','ascii'))
print locale.format('%.2f', 5554435.555, 3)
output
Code:
# ./test.py
5,554,435.55
|
|
|
|
09-24-2008, 07:35 AM
|
#6
|
|
Senior Member
Registered: Jul 2004
Location: Nantes (France)
Distribution: Arch Linux
Posts: 1,897
Rep:
|
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?
Yves.
|
|
|
|
09-24-2008, 07:41 AM
|
#7
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,026
|
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
Last edited by chrism01; 09-24-2008 at 07:44 AM.
|
|
|
|
09-25-2008, 02:05 AM
|
#8
|
|
LQ Newbie
Registered: Jul 2007
Posts: 7
Original Poster
Rep:
|
Quote:
Originally Posted by chrism01
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:
1 = 1
10 = 10
100 = 100
1000 = 1,000
10000 = 10,000
100000 = 100,000
1000000 = 1,000,000
etc.
|
|
|
|
09-25-2008, 08:29 AM
|
#9
|
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
I think this will do the trick for you, unless your Cygwin doesn't have sed or, possibly, GNU sed:
Code:
$ echo " 1234567890 " | sed -r ':L;s=\b([0-9]+)([0-9]{3})\b=\1,\2=g;t L'
1,234,567,890
Thank you for the interesting Q -- I got me to look at & use sed labels & loops for the 1st time.
Anticipating your Q's:
http://www.gnu.org/software/sed/manual/sed.html
":L": http://www.gnu.org/software/sed/manu...mming-Commands
"-r": http://www.gnu.org/software/sed/manu...02dextended-30
"\b": http://www.gnu.org/software/sed/manual/sed.html#Escapes
"=": http://www.gnu.org/software/sed/manu...s_0022-Command (para 1)
 (Cheshire Cat imitation)
|
|
|
1 members found this post helpful.
|
09-25-2008, 11:22 AM
|
#10
|
|
LQ Newbie
Registered: Jul 2007
Posts: 7
Original Poster
Rep:
|
Bravo sir, that works perfectly. It will take me a while to wrap my head around though  . Thank you very much.
|
|
|
|
09-27-2008, 05:42 AM
|
#11
|
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
You're welcome.
As to "wrapping your head around it", I figured it might be unfamiliar which is why I gave all the links. I'll be happy to answer any specific Q's.
|
|
|
|
02-23-2011, 08:36 PM
|
#12
|
|
LQ Newbie
Registered: Feb 2011
Location: Georgia, USA
Distribution: xubuntu, Mac OS X
Posts: 5
Rep:
|
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.
|
|
|
|
02-24-2011, 04:24 AM
|
#13
|
|
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,314
|
Quote:
Originally Posted by turtlegeek
So I spent a few minutes to find the solution below. Since my Mac only allows Basic Regular Expression (BRE)...
|
In such cases I sometimes download the GNU version and compile it on the Mac. Several commands have slight variations in the GNU version.
|
|
|
|
02-24-2011, 08:14 AM
|
#14
|
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
turtlegeek,
Welcome to LQ -- good 1st post.
- 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'
Last edited by archtoad6; 02-24-2011 at 08:19 AM.
|
|
|
1 members found this post helpful.
|
02-24-2011, 08:16 AM
|
#15
|
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
acc-dupe trying to edit.
Last edited by archtoad6; 02-24-2011 at 08:20 AM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:15 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|