简体   繁体   English

Ruby:从顶部-n1输出获取CPU(un)

[英]Ruby: Get the CPU from the top -n1 output (un)

I want to save the output from this for example: 我想保存此输出,例如:

%Cpu(s):  9,0 be,  2,5 sy,  0,2 ni, 88,2 un,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st

into a interger, but I only need the number before the "un" chars. 到一个整数,但是我只需要在“ un”字符之前的数字。 It's the output of top -n1 of a Linux Server. 它是Linux服务器top -n1的输出。 The numbers of the output change every second. 输出数字每秒更改一次。 Btw. 顺便说一句。 sry for my bad english... 抱歉我的英语不好...

An integer cannot hold a fraction number like 88,2 . 整数不能包含分数88,2 For a floating point number, you could use 对于浮点数,可以使用

output = "%Cpu(s):  9,0 be,  2,5 sy,  0,2 ni, 88,2 un,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st"
number = output[/[0-9,]* un/].tr(',', '.').to_f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM