Reputation: 1784
what I want to do is take the command uptime, and get the load averages
$ uptime
07:01:30 up 20:29, 2 users, load average: 0.32, 0.39, 0.54
I have a feeling this is something I can do with awk, but I am not quite sure how. pls assist.
Upvotes: 5
Views: 6057
Reputation: 4963
Simple awk
oneliner:
uptime | awk -F: '{print $NF}'
1.97, 2.06, 2.47
Or without leading space:
uptime | awk -F': ' '{print $NF}'
1.89, 2.04, 2.46
Another option is to use bash parameter expansion:
echo ${$(uptime)[@]: -3}
1.79, 2.10, 2.08
Upvotes: 0
Reputation: 4947
just throw this into a main.c
file and compile it with gcc or another c compiler:
#include <stdlib.h>
#include <stdio.h>
int main(void) {
double loadavg[3];
getloadavg(loadavg, 3);
printf("%.2f, %.2f, %.2f\n", loadavg[0], loadavg[1], loadavg[2]);
return EXIT_SUCCESS;
}
gcc -o loadavg main.c
now just do a simple chmod +x loadavg
and do a move loadavg /usr/local/bin
and voila, if /usr/local/bin
is in your path, you can now enjoy a loadavg command that returns the exact same format as the uptime command.
if you want to get more info about that getloadavg
c function, just head to this link: https://linux.die.net/man/3/getloadavg
getloadavg - get system load averages
#define _BSD_SOURCE /* See feature_test_macros(7) */#include <stdlib.h>
int getloadavg(double loadavg[], int nelem);
The getloadavg() function returns the number of processes in the system run queue averaged over various periods of time. Up to nelem samples are retrieved and assigned to successive elements of loadavg[]. The system imposes a maximum of 3 samples, representing averages over the last 1, 5, and 15 minutes, respectively.
If the load average was unobtainable, -1 is returned; otherwise, the number of samples actually retrieved is returned.
This function is available in glibc since version 2.2.
Not in POSIX.1-2001. Present on the BSDs and Solaris.
Upvotes: 0
Reputation: 4264
Uptime takes the load average from /proc/loadavg
(as you already mentioned in your comment), where the three numbers are 1-minute, 5-minute, and 15-minute averages.
So, a faster solution without involving uptime
would be
awk '{print $1}' < /proc/loadavg
which would print you the 1-minute average, etc.
Upvotes: 0
Reputation: 1406
Assuming that uptime
will print the load averages last, you can keep only the last 17 characters:
uptime | tail -c 17
Upvotes: 0
Reputation: 3646
You can do this with Bash.
up=($(uptime))
load_avg=${up[@]: -3}
Or if you're reading from /proc/loadavg
read -a load < /proc/loadavg
load_avg=${load[@]:0:3}
Upvotes: 3
Reputation: 203149
$ uptime | awk -F': ' '{print $2}'
0.32, 0.39, 0.54
$ uptime | sed 's/.*: //'
0.32, 0.39, 0.54
Upvotes: 3
Reputation: 25865
You can use grep
uptime | grep -o 'load.*'
Also you can extract the three load average fields separately by
uptime | grep -o '[0-9]\+\.[0-9]\+*'
Upvotes: 3
Reputation: 2373
You can use a regex with backreferences: i.e. find any sequence of characters (.*) but only at a point directly after average:
uptime | grep -oP '(?<=average:).*'
Upvotes: 7
Reputation: 622
Found a solution here.
uptime | awk -F'[a-z]:' '{ print $2}'
Upvotes: 3
Reputation: 33046
Assuming that uptime
will always output data with the format above, you can cut at :
and keep the fifth group:
$ uptime | cut -d : -f 5
0,24, 0,23, 0,23
Upvotes: 2