5

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.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
j0h
  • 1,675
  • 6
  • 27
  • 50
  • I went into man uptime and found the /proc/loadavg file, then did: $ awk '{print $1,$2,$3;}' loadavg 0.17 0.19 0.29 which got me what I was looking for. I will still look at the responses, the variation is neat. – j0h Jul 19 '14 at 11:29

10 Answers10

7

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:).*'
hillel
  • 2,343
  • 2
  • 18
  • 25
3

Found a solution here.

uptime | awk -F'[a-z]:' '{ print $2}'
name not found
  • 622
  • 4
  • 13
3

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]\+*'
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
3
$ uptime | awk -F': ' '{print $2}'
0.32, 0.39, 0.54

$ uptime | sed 's/.*: //'
0.32, 0.39, 0.54
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
2

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
Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
2

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}
John B
  • 3,566
  • 1
  • 16
  • 20
0

Assuming that uptime will print the load averages last, you can keep only the last 17 characters:

uptime | tail -c 17
hlorand
  • 1,070
  • 10
  • 8
0

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.

Simon A. Eugster
  • 4,114
  • 4
  • 36
  • 31
0

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)

GottZ
  • 4,824
  • 1
  • 36
  • 46
0

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
Zlemini
  • 4,827
  • 2
  • 21
  • 23