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

Try:

Percent=$(bc << %%
   scale=4
   ( ${used} / ${avail} ) * 100
   quit
 %%)

genius.....don't know what it all means but it works.

Cheers and beers,
Neil

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.

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.

From: HANDY ONE-LINERS FOR SED

The shell can do integer arithmetic, you don't need to use expr:-

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

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.

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.

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.

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.