j0h
j0h

Reputation: 1784

grep out load average from uptime

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

Answers (10)

Zlemini
Zlemini

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

GottZ
GottZ

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


quotation as of writing this answer:

getloadavg(3) - Linux man page

Name

getloadavg - get system load averages

Synopsis

#define _BSD_SOURCE         /* See feature_test_macros(7) */#include <stdlib.h>
int getloadavg(double loadavg[], int nelem);

Description

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.

Return Value

If the load average was unobtainable, -1 is returned; otherwise, the number of samples actually retrieved is returned.

Versions

This function is available in glibc since version 2.2.

Conforming to

Not in POSIX.1-2001. Present on the BSDs and Solaris.

See Also

uptime(1), proc(5)

Upvotes: 0

Simon A. Eugster
Simon A. Eugster

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

hlorand
hlorand

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

John B
John B

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

Ed Morton
Ed Morton

Reputation: 203149

$ uptime | awk -F': ' '{print $2}'
0.32, 0.39, 0.54

$ uptime | sed 's/.*: //'
0.32, 0.39, 0.54

Upvotes: 3

Jayesh Bhoi
Jayesh Bhoi

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

hillel
hillel

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

name not found
name not found

Reputation: 622

Found a solution here.

uptime | awk -F'[a-z]:' '{ print $2}'

Upvotes: 3

Stefano Sanfilippo
Stefano Sanfilippo

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

Related Questions