grep out load average from uptime

*爱你&永不变心* 提交于 2020-01-30 07:44:05

问题


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.


回答1:


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:).*'



回答2:


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]\+*'



回答3:


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

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



回答4:


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



回答5:


Found a solution here.

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



回答6:


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}



回答7:


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

uptime | tail -c 17


来源:https://stackoverflow.com/questions/24839694/grep-out-load-average-from-uptime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!