使用正则表达式提取和处理 GPU 温度信息 [英] Extracting and Processing GPUTemperature Information Using Regexp

查看:22
本文介绍了使用正则表达式提取和处理 GPU 温度信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 Linux Ubuntu 应用程序、传感器中提取并处理显卡温度整数以下输出:

I need to extract and process graphic card temperature integer the following output from Linux Ubuntu application, sensors:

amdgpu-pci-0c00
Adapter: PCI adapter
fan1:        1972 RPM
temp1:        +50.0°C  (crit =  +0.0°C, hyst =  +0.0°C)

amdgpu-pci-0600
Adapter: PCI adapter
fan1:        1960 RPM
temp1:        +47.0°C  (crit =  +0.0°C, hyst =  +0.0°C)

amdgpu-pci-0200
Adapter: PCI adapter
fan1:        1967 RPM
temp1:        +52.0°C  (crit =  +0.0°C, hyst =  +0.0°C)

pch_skylake-virtual-0
Adapter: Virtual device
temp1:        +33.0°C

amdgpu-pci-0900
Adapter: PCI adapter
fan1:        1893 RPM
temp1:        +51.0°C  (crit =  +0.0°C, hyst =  +0.0°C)

amdgpu-pci-0300
Adapter: PCI adapter
fan1:        1992 RPM
temp1:        +53.0°C  (crit =  +0.0°C, hyst =  +0.0°C)

coretemp-isa-0000
Adapter: ISA adapter
Package id 0:  +24.0°C  (high = +80.0°C, crit = +100.0°C)
Core 0:        +23.0°C  (high = +80.0°C, crit = +100.0°C)
Core 1:        +21.0°C  (high = +80.0°C, crit = +100.0°C)

假设我想提取与 amd gpu 温度相关的信息,即 50、47、52、51 和 53.到目前为止,我正在执行以下代码:

Suppose I want to extract information that associates with amd gpu temperature, which are 50, 47, 52, 51, and 53. So far what I have is executing the following code:

sensors|grep temp| grep -Eo '\+[0-9]{0,9}'

我得到了:

+50
+0
+0
+47
+0
+0
+52
+0
+0
+32
+51
+0
+0
+53
+0
+0

所以我需要弄清楚:

  1. 正则表达式环视断言,以便它捕获数字开头带有 + 号的整数,而不显示 +(加号).
  2. 一种只抓取 amdgpu 信息的方法,这样它就不会抓取其他信息.
  3. 一种处理这些温度数字的方法,例如我可以编写一个 bash 脚本来处理这些数字,而如果温度低于 30,则执行此操作,如果高于 70,则执行此操作.我应该将结果放在一个数组中并进行循环,还是有其他实用的方法?

请帮忙.问候

推荐答案

您已经将所需的温度存储在一个数组中,然后您可以用它们进行数学计算.

There you have desired temperatures stored in an array, you can then do the math with them.

arr=( $( IFS=$'\n' gawk 'BEGIN{ RS="\n\n"} { if($0 ~/amdgpu/) print $0 }' test.txt | gawk 'BEGIN{ FS="[+.]" } { if($1 ~/temp1:/) 打印 $2 }' ) )回声${arr[*]}"50 47 52 51 53

test.txt 包含您的示例输出.从传感器命令获取输入(未测试)

test.txt contains your sample output. Getting input from sensors command (not tested)

arr=( $(sensors | IFS=$'\n' gawk 'BEGIN{ RS="\n\n"} { if($0 ~/amdgpu/) print $0 }' | gawk 'BEGIN{ FS="[+.]" } { if($1 ~/temp1:/) 打印 $2 }' ) )回声${arr[*]}"50 47 52 51 53

这篇关于使用正则表达式提取和处理 GPU 温度信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆