我需要从 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
所以我需要弄清楚:
请帮忙。
问候
最佳答案
在那里,您将所需的温度存储在一个数组中,然后您可以对它们进行数学运算。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/