Regex 使用脚本提取Ubuntu传感器命令
这个问题是问题的继续 基本上,我想使用sensors命令和gawk和bash等脚本提取gpu温度信息 传感器输出示例如下所示:Regex 使用脚本提取Ubuntu传感器命令,regex,bash,ubuntu,awk,Regex,Bash,Ubuntu,Awk,这个问题是问题的继续 基本上,我想使用sensors命令和gawk和bash等脚本提取gpu温度信息 传感器输出示例如下所示: 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:
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)
我想根据总线ID的升序打印每个GPU及其标签和温度。例如,根据之前的输出,第一个GPU GPU0将是标签为amdgpu-pci-0200的GPU,GPU1将是amdgpu-pci-0300,直到最后一个GPU4是amdgpu-pci-0c00。这不包括coretemp-isa-0000,因为它不是GPU。无论总线ID是连续的还是跳过的,我都希望以升序的方式对其进行标记
下面的bash代码将提取所有GPU temp,而不正确地进行排序
#!/bin/bash
while [ 1 ]
do
temp=( $( sensors | IFS=$'\n' gawk 'BEGIN{ RS="\n\n"} { if($0 ~ /amdgpu/) print $0 }' | gawk 'BEGIN{ FS="[+.]" } { if($1 ~ /temp1:/) print $2 }' ))
let j=0
for i in "${temp[@]}"
do
echo -en "GPU $j temp is $i \r "
j=$(($j +1))
sleep 1
done
done
我该怎么处理呢
关于您没有提供实际的预期输出,也没有提供有关如何解析输入的一些详细信息,因此这有点猜测,但可能正是您想要的:
$ cat tst.sh
#!/bin/env bash
#while :
#do
cat file |
gawk '
BEGIN { RS="" }
$1 ~ /amdgpu/ {
temp = "N/A"
for (i=1; i<NF; i++) {
if ($i == "temp1:") {
temp = gensub(/^[^0-9]*([0-9]+).*/,"\\1",1,$(i+1))
}
}
temps[$1] = temp
}
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (id in temps) {
print "GPU" (++cnt), id, temps[id]
}
}
'
#sleep 1
#done
$ ./tst.sh
GPU1 amdgpu-pci-0200 52
GPU2 amdgpu-pci-0300 53
GPU3 amdgpu-pci-0600 47
GPU4 amdgpu-pci-0900 51
GPU5 amdgpu-pci-0c00 50
$cat tst.sh
#!/bin/env bash
#而:
#做
cat文件|
呆呆的
开始{RS=”“}
$1~/amdgpu/{
temp=“不适用”
对于(i=1;i假设传感器输出的示例如下:
是您请求帮助的工具的输入。请提供您试图编写的脚本的准确预期输出。解释我们如何知道coretep-isa-0000
不是GPU,以及为什么shell脚本中有sleep 1
,以及y它被放置在传感器输出的数组内容的循环中,而不是周围的无限循环中。GPU temp是一个带有GPU文本的循环,而coretemp-isa-0000没有。显然,您在编辑的答案中找到了它。脚本无限运行,不停地讲述温度,这就是为什么它需要1秒延迟f的原因这很好,但是我需要使用排序的temps数组,这样我就可以使用bash脚本来操作它。这样数组就会像:temps[0]=52,temps[1]=53,…,temps[4]=50.对不起,我不清楚问一个新问题,这次要清楚。即使有你的评论,我仍然不知道你到底想要输出什么。好的,我刚刚创建了一个新问题