Operating System - HP-UX
1820540 Members
3100 Online
109626 Solutions
New Discussion юеВ

Re: rounding up in awk/shell

 
SOLVED
Go to solution
Stefan Farrelly
Honored Contributor

rounding up in awk/shell

Hi all,

our usual memory.sh script which uses adb and the kernel to display the system memory total works fine on 10.20 and 11.0 but on 11.11 it computes to an integer instead of a round number, eg;

echo "phys_mem_pages/D" | adb /stand/vmunix /dev/kmem|grep phys|tail -1|awk '{printf $2/256}'

On our server with 768Mb of RAM shows 767.992

My question is how can we round this up in awk (should display 768) or using prinf. Ive tried but cant figure it. I know this can be done the long way in a shell script or probably in perl, definitely in C using round, but Im after a 1 liner using awk and/or printf to do it - and so that it works on servers with different memory sizes.

Thanks,

Stefan
Im from Palmerston North, New Zealand, but somehow ended up in London...
13 REPLIES 13
Jean-Luc Oudart
Honored Contributor

Re: rounding up in awk/shell

Hi

echo "767.99" | awk '{printf("%d\n",$1 + 0.5);}'
768
echo "767.49" | awk '{printf("%d\n",$1 + 0.5);}'
767
echo "767.51" | awk '{printf("%d\n",$1 + 0.5);}'
768

Jean-Luc
fiat lux
Dietmar Konermann
Honored Contributor

Re: rounding up in awk/shell

What about

echo "phys_mem_pages/D" | adb /stand/vmunix /dev/kmem|tail -1|awk '{printf "%d",$2/256+.5}'

BTW, I dropped the grep... it failed on my 11.11 box.

Regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Stefan Farrelly
Honored Contributor

Re: rounding up in awk/shell


Hey, come on guys, no simply adding 1 or .5 or whatever to it. I want to round up. Must be able to round with awk, or nawk, or bc or dc or bs ??? anyone ?
Im from Palmerston North, New Zealand, but somehow ended up in London...
Dietmar Konermann
Honored Contributor

Re: rounding up in awk/shell

Hmmm, Stefan.

What's the differnence between rounding up X and rounding down (X+.5)? Why should a designer include such a redundant functionality? :)

Regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
H.Merijn Brand (procura
Honored Contributor
Solution

Re: rounding up in awk/shell

why awk?

# echo 767.992 | perl -ne'printf"%.0f\n",$_'
768
#
Enjoy, Have FUN! H.Merijn
Stefan Farrelly
Honored Contributor

Re: rounding up in awk/shell


sorry, im purely being pedandtic. I want to round up or down using awk or a shell script in a one liner if possible. You can do in C, probably in perl, but not in a simple shell script/command ?
Im from Palmerston North, New Zealand, but somehow ended up in London...
Stefan Farrelly
Honored Contributor

Re: rounding up in awk/shell

Hi Procura,

That did it! you dont need perl - your printf function is rounding perfectly (>.5 = up, < .5 round down). This now works;

echo "phys_mem_pages/D" | adb /stand/vmunix /dev/kmem|grep phys|tail -1|awk '{printf "%.0f MB\n",$2/256}'

Thanks!

Stefan
Im from Palmerston North, New Zealand, but somehow ended up in London...
Jean-Luc Oudart
Honored Contributor

Re: rounding up in awk/shell

As far as I knwo round does not exist (in awk)
Well , I build it
You may like it or Not, just try to help

echo $1 | awk '
function round(aa) {
return int( aa + 0.5 )
}
{
printf("%d\n",round($1));
}'

Jean-Luc
fiat lux
Pete Randall
Outstanding Contributor

Re: rounding up in awk/shell

Stefan,

Thankyou for asking. I've just been accepting the answer as is and rounding up myself. This is much better.

Eternally grateful,
Pete

Pete
Tom Danzig
Honored Contributor

Re: rounding up in awk/shell

To round up using awk:
echo "767.992" | awk '{printf("%5.0f\n",$0)}'

To round down using awk:
echo "767.992" | awk '{printf("%d\n",$0)}'

Tom Danzig
Honored Contributor

Re: rounding up in awk/shell

To be more specific in my previous post, Using %5.0f actually will properly round the number up or down as expected. Using %d simply truncates off the decimals printing only the integer portion.
Stephen Serbe
Occasional Advisor

Re: rounding up in awk/shell

Isn't the forum great...
I had the same question and found my answer(s) right here using search. Thanks, Stephen
A man without a smiling face should not open shop
Pete Randall
Outstanding Contributor

Re: rounding up in awk/shell

Psssst - Stephen!!! That's Stefan!

;^)

Pete

Pete