我用它來顯示我的CPU溫度。

echo CPU溫度>> /home/andy/bin/HD_AND_CPU_TEMPS.txt

sensors -f | grep "temp1" >> /home/andy/bin/HD_AND_CPU_TEMPS.txt
geany /home/andy/bin/HD_AND_CPU_TEMPS.txt

radeon 是我的圖形 CPU。

radeon-pci-0008
Adapter: PCI adapter
temp1:       +115.1°F  (crit = +248.0°F, hyst = +213.6°F)

k10temp-pci-00c3
Adapter: PCI adapter
temp1:        +82.4°F  (high = +158.0°F)
                       (crit = +169.5°F, hyst = +168.3°F)

我希望該文件僅顯示第二個溫度。

但有兩個temp1?

如何只顯示第二個溫度?

答案1

以下程式碼temp1僅當該行位於包含以下內容的行之後時才列印該行k10temp-pci-00c3

$ sensors -f | awk '/k10temp-pci-00c3/{f=1} f && /temp1/{print; f=0}'
temp1:        +82.4°F  (high = +158.0°F)

如果您還想要標題:

$ cat sensors-f | awk 'BEGIN{print"CPU Temperature"} /k10temp-pci-00c3/{f=1} f && /temp1/{print; f=0}'
CPU Temperature
temp1:        +82.4°F  (high = +158.0°F)

怎麼運作的

  • BEGIN{print"CPU Temperature"}列印標題。

  • /k10temp-pci-00c3/{f=1}當找到f包含的行時,將 awk 變數設為1 (true)。k10temp-pci-00c3

  • f && /temp1/{print; f=0}f如果為 true 且該行包含 ,則會列印一行temp1。這也會設定f回零(假)。

替代方案:使用 sed

$ sensors -f | sed -n '/k10temp-pci-00c3/,/temp1/{/temp1/p}'
temp1:        +82.4°F  (high = +158.0°F)

答案2

您可以將晶片名稱作為參數傳遞sensors k10temp-pci-00c3給 let感應器軟體僅讀取該特定感測器。

$ sensors k10temp-pci-00c3
Adapter: PCI adapter
temp1:        +82.4°F  (high = +158.0°F)
              (crit = +169.5°F, hyst = +168.3°F)

與讀取所有可用感測器然後限制輸出相比,這將導致讀取速度更快。

然後你只想取得 temp1 行,最簡單的方法就是使用 grep

$ sensors k10temp-pci-00c3 | grep temp1
temp1:        +82.4°F  (high = +158.0°F)

僅獲取溫度:使用 awk 將該行視為列並獲取第二列

$ sensors k10temp-pci-00c3 | grep temp1 | awk '{print $2}'
+82.4°F

我相信這種方法運行速度更快並且使用更少的處理能力。

對於使用不同機器的其他人,感測器輸出可能不同,相應地更改 grep 的內容。在我的機器上,感測器標籤Tdietemp1

相關內容