The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 12:39 PM
formating output Krrishv Shell Programming and Scripting 20 02-02-2007 09:39 AM
formating date ragha81 Shell Programming and Scripting 2 01-05-2007 11: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 11:58 AM
Disk formating DPAI UNIX for Dummies Questions & Answers 1 11-28-2001 08:06 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-24-2004
nhatch nhatch is offline
Registered User
  
 

Join Date: Aug 2002
Location: London, England
Posts: 84
formating number strings

I have treid searching for what I thought would be a common question...but to no avail.


I have to long numbers (4-8) digits. how can I format them so they are printed as 1,234,567 rather than 1234567? I have no idea where to start, have played with varying types of awk, print, substr.

Also, pecentages.....if you have total amount and amount used how, using expr, can you work out the percentage used? I get 0 as the value.

pecent=`expr $used / $avail \* 100`

percent, used, and avail have all been set previously as an integer value.

PS. I'm no mathsmatitiojkljknjn.....how ever you spell it, so my logic may be wrong.

Cheers,
Neil
  #2 (permalink)  
Old 03-24-2004
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Try:
Code:
Percent=$(bc << %%
   scale=4
   ( ${used} / ${avail} ) * 100
   quit
 %%)
  #3 (permalink)  
Old 03-24-2004
nhatch nhatch is offline
Registered User
  
 

Join Date: Aug 2002
Location: London, England
Posts: 84
genius.....don't know what it all means but it works.

Cheers and beers,
Neil
  #4 (permalink)  
Old 03-24-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Neither used nor avail sounds like a total and you need a total. But, for example 2 is 50% of 4. My mathematical training enables me to construct:
expr 2 \* 100 / 4
which will result in 50.

Since you are using expr, you must be using a weak shell. One solution for formatting numbers might be sed:
echo 1234567 | sed 's/\(.\)\(...\)\(...\)/\1,\2,\3/'
which will only work for 7 digit numbers. You will need similiar sed expressions for other number lengths and you will need to select the right one with if statements.
  #5 (permalink)  
Old 03-24-2004
nhatch nhatch is offline
Registered User
  
 

Join Date: Aug 2002
Location: London, England
Posts: 84
many thanks.......

This may not be allowed V's questions........

But I'm using ksh....is this a weak shell? if it is what makes it so......I'm always looking to improve my scripting and if changing the shell or the language is required, then so be it.
  #6 (permalink)  
Old 03-24-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
From: HANDY ONE-LINERS FOR SED
Quote:
# add commas to numeric strings, changing "1234567" to "1,234,567"
gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' # GNU sed
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # other seds

# add commas to numbers with decimal points and minus signs (GNU sed)
gsed ':a;s/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g;ta'
  #7 (permalink)  
Old 04-28-2009
genedc1 genedc1 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 3
The one-liner is great and I understand what it does in the end, like putting a comma in as a delimiter. What I need, being a newb, is a breakdown of what each part of the one-liner is doing. I understand up until the substitution. Can anyone break this down for me? Because I would really like to understand the commands I'm planning on using.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0