regex - 使用 Regexp 提取和处理 GPU 温度信息

标签 regex bash ubuntu

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

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。到目前为止,我所拥有的是执行以下代码:
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

所以我需要弄清楚:
  • 正则表达式环视断言,以便它捕获在数字开头带有 + 符号的整数,而不显示 +(加号)符号。
  • 一种只获取 amdgpu 信息的方法,这样它就不会获取其他信息。
  • 一种处理这些温度数字的方法,例如我可以编写一个 bash 脚本来处理这些数字,而如果温度低于 30,则执行此操作,如果超过 70,则执行此操作。我应该把结果放在一个数组中并做一个循环,还是有其他实用的方法?

  • 请帮忙。
    问候

    最佳答案

    在那里,您将所需的温度存储在一个数组中,然后您可以对它们进行数学运算。
    arr=( $( IFS=$'\n' gawk 'BEGIN{ RS="\n\n"} { if($0 ~ /amdgpu/) print $0 }' test.txt | gawk 'BEGIN{ FS="[+.]" } { if($1 ~ /temp1:/) print $2 }' ) ) echo "${arr[*]}" 50 47 52 51 53
    test.txt 包含您的示例输出。从传感器命令获取输入(未测试)
    arr=( $( sensors | IFS=$'\n' gawk 'BEGIN{ RS="\n\n"} { if($0 ~ /amdgpu/) print $0 }' | gawk 'BEGIN{ FS="[+.]" } { if($1 ~ /temp1:/) print $2 }' ) ) echo "${arr[*]}" 50 47 52 51 53

    关于regex - 使用 Regexp 提取和处理 GPU 温度信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49985716/

    相关文章:

    regex - 为特定网址强制使用 HTTPS

    regex - 使用正则表达式从 LDAP 路径中提取名称

    bash - 字段是不可变的 k8s

    python - 使用 pyscard 读取序列号 mifare

    java - 正则表达式更改 eclipse 中属性的大小写

    python:在带有正则表达式的字符串中的单词之间插入逗号分隔符

    bash - 如何在 bash 中构建条件分配?

    bash - 如何访问 eval 命令的 bash PIPESTATUS 数组?

    ubuntu - PyQt4 的核心转储

    ubuntu - 使用 ffmpeg 和 nginx 直播 .webm 文件