The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Output formating jaydeep_sadaria Shell Programming and Scripting 1 04-10-2008 09:39 AM
formating output Krrishv Shell Programming and Scripting 20 02-02-2007 06:39 AM
formating date ragha81 Shell Programming and Scripting 2 01-05-2007 08:20 AM
How to concatenate two strings or several strings into one string in B-shell? fontana Shell Programming and Scripting 2 08-26-2005 08:58 AM
Disk formating DPAI UNIX for Dummies Questions & Answers 1 11-28-2001 05:06 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 03-24-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Since you were using expr, I assumed that you were using sh which cannot do arithmetic. ksh is a great shell, but you're not using all of its features yet.

With ksh you can do:

((percent = 100 * used / avail))

which will work the same as

((percent = 100 * $used / $avail))

if "avail" and "used" both contain simple numbers.
Reply With Quote
Forum Sponsor
  #9  
Old 03-24-2004
TioTony's Avatar
Bit Pusher
 

Join Date: Oct 2001
Location: Southern California
Posts: 332
I'm not infront of a box I can test but it seems logical to me that printf or one of it's variants may have the thousands comma separator functionallity Neil was looking for. If anyone knows for sure, post back otherwise I will try to figure it out tomorrow if I have time.
Reply With Quote
  #10  
Old 03-25-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Quote:
Originally posted by Ygor

# add commas to numeric strings, changing "1234567" to "1,234,567"
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # other seds
That's pretty awesome. But it's more powerful than the comments seem to indicate. It handles leading minus signs. It also handles a decimal point followed by up to three decimal places. It not obvious how to handle commas with more than three decimal places. Leave them alone? Like this: "1,234,567,890.0000" ?

If that is right, we can extend the sed one-liner:

sed -e :a -e 's/^\([^.]*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'

This countinues to handle leading minus, plus, and dollar signs. In the accounting world, I've seen an odd convention of putting numbers in parentheses. That also works.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 08:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0