使用 grep/regex 获取温度数组

使用 grep/regex 获取温度数组

我正在尝试在我的服务器上设置一些基本的温度监控 -(不使用第三方工具)。

我已经在我的 Linux 机器上安装了几个库以使传感器在我的服务器上工作,现在我可以使用sensors可能返回如下数据的命令:

asb100-i2c-1-2d
Adapter: SMBus nForce2 adapter at 5500
in0:          +1.79 V  (min =  +1.39 V, max =  +2.08 V)
in1:          +1.79 V  (min =  +1.39 V, max =  +2.08 V)
in2:          +3.34 V  (min =  +2.96 V, max =  +3.63 V)
in3:          +2.96 V  (min =  +2.67 V, max =  +3.28 V)
in4:          +3.06 V  (min =  +2.51 V, max =  +3.79 V)
in5:          +3.06 V  (min =  +0.00 V, max =  +0.00 V)
in6:          +3.04 V  (min =  +0.00 V, max =  +0.00 V)
fan1:        6136 RPM  (min = 2777 RPM, div = 2)
fan2:           0 RPM  (min = 3534 RPM, div = 2)
fan3:           0 RPM  (min = 10714 RPM, div = 2)
temp1:        +37.0°C  (high = +80.0°C, hyst = +75.0°C)
temp2:        +26.5°C  (high = +80.0°C, hyst = +75.0°C)
temp3:         -0.5°C  (high = +80.0°C, hyst = +75.0°C)
temp4:        +25.0°C  (high = +80.0°C, hyst = +75.0°C)
cpu0_vid:    +1.750 V

w83l785ts-i2c-1-2e
Adapter: SMBus nForce2 adapter at 5500
temp1:        +30.0°C  (high = +85.0°C)

| grep temp然后我意识到我可以通过在命令末尾添加来轻松缩小范围,因此我尝试运行sensors | grep temp,并得到了以下结果:

temp1:        +37.0°C  (high = +80.0°C, hyst = +75.0°C)
temp2:        +26.5°C  (high = +80.0°C, hyst = +75.0°C)
temp3:         -0.5°C  (high = +80.0°C, hyst = +75.0°C)
temp4:        +25.0°C  (high = +80.0°C, hyst = +75.0°C)
temp1:        +30.0°C  (high = +85.0°C)

我意识到 temp3 显然没有正常运行,因此我修改了命令以消除该结果:sensors | grep temp[1,2,4]

temp1:        +36.0°C  (high = +80.0°C, hyst = +75.0°C)
temp2:        +26.5°C  (high = +80.0°C, hyst = +75.0°C)
temp4:        +25.0°C  (high = +80.0°C, hyst = +75.0°C)
temp1:        +30.0°C  (high = +85.0°C)

现在我想将其修剪掉,所以我所得到的只是一个逗号分隔的字符串,它可能看起来像这样。

+36.0,+26.5,+25.0,+30.0

然后我可以将其设置为每 5/10 分钟将数据提交到服务器一次。

我如何使用grep或其他命令来实现这一点?

答案1

使用awk

sensors | awk '/temp[124]/ {sub("°C", "", $2); print($2)}'

或者用逗号分隔:

sensors | awk -v ORS=, '/temp[124]/ {sub("°C", "", $2); print($2)}' | sed 's/,$//'

相关内容