Reputation: 7332
I have a simple bash script which is getting the load average using uptime and awk, for example
LOAD_5M=$(uptime | awk -F'load averages:' '{ print $2}' | awk '{print $2}')
However this includes a ',' at the end of the load average
e.g.
0.51,
So I have then replaced the comma with a string replace like so:
LOAD_5M=${LOAD_5M/,/}
I'm not an awk or bash wizzkid so while this gives me the result I want, I am wondering if there is a succinct way of writing this, either by:
Upvotes: 2
Views: 223
Reputation: 158060
The 5 min load is available in /proc/loadavg
. You can simply use cut
:
cut -d' ' -f2 /proc/loadavg
With awk
you can issue:
awk '{print $2}' /proc/loadavg
If you are not working on Linux the file /proc/loadavg
will not being present. In this case I would suggest to use sed
, like this:
uptime | sed 's/.*, \(.*\),.*,.*/\1/'
Upvotes: 1
Reputation: 8134
The load average numbers are always the last 3 fields in the 'uptime' output so:
IFS=' ,' read -a uptime_fields <<<"$(uptime)"
LOAD_5M=${uptime_fields[@]: -2:1}
Upvotes: 0
Reputation: 785296
You can do that in same awk command:
uptime | awk -F 'load averages?: *' '{split($2, a, ",? "); print a[2]}'
1.32
Upvotes: 2
Reputation: 10865
uptime | awk -F'load average:' '{ print $2}' | awk -F, '{print $2}'
0.38
(My uptime output has 'load average:' singular)
Upvotes: 0