Linux

使用 grep/regex 獲取溫度數組

  • January 3, 2012

我正在嘗試在我的伺服器上設置一些基本的溫度監控 - (不使用第三方工具)。

我在我的 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或其他命令實現此目的?

使用awk

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

或逗號分隔:

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

引用自:https://serverfault.com/questions/346171