tags:

views:

1135

answers:

5

Hi,

I am trying to get the rounded number of the average load in the past 5 mins. So here goes my command:

 uptime | awk -F, '{print $5}'|printf "%.0f\n"

It seems incorrect as it always give me 0.

If I tried to use a variable as intermediate between awk and printf, then it is correct

 avgload=$(uptime | awk -F, '{print $5}')

 printf "%.0f\n" $avgload

So anything wrong with my first try?

Thanks and regards!

UPDATE:

Just for getting the average load in the past 5 mins, here is the output of uptime on my linux server (Kubuntu)

$ uptime
13:52:19 up 29 days, 18 min, 15 users, load average: 10.02, 10.04, 9.58

On my laptop (Ubuntu) it is similar

`$ uptime

13:53:58 up 3 days, 12:02, 8 users, load average: 0.29, 0.48, 0.60 `

That's why I take the 5th field.

+1  A: 

You can just do

printf "%.0f\n" `uptime | awk -F, '{print $5}'`

The backticks are essentially command substitution, so whatever the output of uptime | awk -F, '{print $5}' is, will be the argument to printf.

The problem with the first approach is that printf just does not accept arguments from stdin; if it did take arguments from stdin, then it would have worked fine. Also, no arguments to printf apparently does mean "put zero in for my arguments".

$ echo hello | printf "%s"

$ printf "%s" hello
hello
$ printf "%.0f"
0
Mark Rushakoff
+1  A: 

I don't think you can feed the input to printf on stdin. Try this version:

printf "%.0f\n" `uptime | awk -F, '{print $5}'`
jbourque
Oops... 9 seconds too slow. :)
jbourque
+5  A: 

The 5th comma-separated field from the uptime output is non-existant (on my system at least), which is why you keep getting zero. The 5-minute uptime is the second-to-last field, so this works:

uptime | awk '{printf "%.0f\n",$(NF-1)}'
Paul
+1 for using `awk` to do the formatting rather than piping its output unnecessarily to another command.
Dennis Williamson
Thanks! (1) what does $(NF-1) mean for awk? (2) The 5th comma-separated field is the 5-minute uptime on my systems.See my update to OP. What is yours like?
Tim
NF-1 is the Number of fields - 1 : http://tomecat.com/jeffy/tttt/awk.html
Adriano Varoli Piazza
+2  A: 

Simplest version (use built-in awk printf - kudos Dennis Williamson):

uptime |awk -F, '{printf "%0.f\n",$5}'

Original answer: Run it through xargs instead.

uptime |awk -F, '{print $5}' |xargs printf "%0.f\n"
Will Bickford
So, what exactly does `xargs` buy you here? Presumably the output of `uptime` is a single line.
Dennis Williamson
xargs tacks the output from awk onto the end of the command line. So you get 'printf "%0.f\n" 0.75', if the output from awk was 0.75.
Will Bickford
Try this command for example:echo "-ltr" |xargs ls
Will Bickford
I know what `xargs` does, but I forgot that printf doesn't accept stdin!
Dennis Williamson
A: 

If you have /proc mounted you can use /proc/loadavg:

LANG=C
read _ uptime _ </proc/loadavg
printf "%.0f\n" $uptime

The above code uses only built-in shell commands. However, if you like, you can use awk too:

 awk '{printf "%.0f\n",$2}' /proc/loadavg
marco