我需要从Linux应用程序(传感器)中提取和处理图形卡温度整数,输出如下:
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所以我需要弄清楚:
请帮帮忙。问候
发布于 2018-04-23 17:21:04
在你想要的温度存储在一个数组中,然后你可以对它们做数学计算。
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
发布于 2018-04-23 18:02:05
如果您愿意使用类似Perl的regexp,也可以使用单个grep获得临时数据:
sensors | grep -oP 'temp\d:\s+\+\K\d+'在temp中,grep后面是一个数字和一个冒号,然后是至少一个空格字符和一个加号,然后我们给出一个lookbehind断言\K,它丢弃它之前的所有内容,最后捕获的只是\d+ (一个或多个数字)。
https://stackoverflow.com/questions/49985716
复制相似问题