LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-21-2007, 11:41 AM   #1
helpmhost
Member
 
Registered: Oct 2006
Posts: 43

Rep: Reputation: 15
Format large numbers with commas at every thousandth decimal spot


Hi,

I wrote a bash script that logs some very large (and occasional very small) numbers. I would like to have commas at every thousandth spot to make it easier to read. I assume there must be some program or built-in command (in bash) that does this, but I can't find it.

Can someone please let me know what the command is? If there is no command, is there a simple way to do this?

Just to make sure everyone understands what I am looking for:
Let's say I have the following number: 45678923

I would like my script to write it as: 45,678,923

Thanks.
 
Old 09-21-2007, 12:53 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Just to get you started, try these things at the command line:
Code:
export LANG=en_US
print "%'d\n" 12345678
Note that the character after the percent sign is a single quote (most likely the key just to the right of the semicolon on your keyboard).

Hope this helps.
 
Old 09-21-2007, 01:02 PM   #3
helpmhost
Member
 
Registered: Oct 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Thanks. That is exactly what I needed.

I am not sure if you just misspelled it, but I used printf and not print.

Thanks.
 
Old 09-22-2007, 08:23 PM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Yep, I missspelled it. Sigh.
 
Old 10-01-2007, 10:39 AM   #5
nwchow
LQ Newbie
 
Registered: Oct 2007
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by wjevans_7d1@yahoo.co View Post
Yep, I missspelled it. Sigh.

How can I do the same thing in ksh?
 
Old 10-01-2007, 10:53 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
This should works the same with ksh.
 
Old 10-01-2007, 11:02 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
printf is an external program, not a builtin in most shells. I'm not sure about all shells, but it's definitely the case in ksh and bash. With this in mind, you will find that executing printf a lot of times in a tight loop will lead to terrible performance, since a new process must be created each time it is called.

In my experience, when you start to hit this level of complexity, it's usually worth switching to using something like perl or python for your program.
 
Old 10-01-2007, 11:05 AM   #8
nwchow
LQ Newbie
 
Registered: Oct 2007
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by jlliagre View Post
This should works the same with ksh.
I tried in ksh. It does not recognize the single quote after the % sign. It said "invalid directive"
 
Old 10-01-2007, 11:54 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by nwchow View Post
I tried in ksh. It does not recognize the single quote after the % sign. It said "invalid directive"
Try using recent a ksh release which supports this syntax with its printf builtin or the external printf command (/bin/printf).

I'm using ksh93s+ and it works.

Code:
$ print ${.sh.version}
Version M 1993-12-28 s+
$ type printf
printf is a shell builtin
$ LC_ALL=en_US printf "%'d\n" 12345678
12,345,678
As a side note. This numerical notation is awful to me, as the comma is the standard decimal separator in the French and probably some other European locales.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting: loop over a file, replacing two decimal numbers frankie_DJ Programming 2 04-30-2007 04:04 PM
rounding a large double to 2 decimal places linuxmandrake Programming 2 03-17-2006 03:31 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
How to use decimal numbers in in shell scripts a1ex_007 Programming 6 03-11-2005 09:05 AM
changing SOAs in named with large numbers of zone files untoldone Linux - Networking 3 08-17-2004 02:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration