我正试图从bash中的lm传感器获取AMD GPU的温度。所以我用尖笛把线调对了。但现在我需要一个正则表达式来从中获取数据
我当前的代码是:
sensors | awk '/edge/ {print$2}'
该输出+53.0°C
现在我只需要53.0。我怎样才能在bash中做到这一点?
没有任何正则表达式,您可以在awk
:中执行此操作
# prints 2nd field from input
awk '{print $2}' <<< 'edge +53.0°C foo bar'
+53.0°C
# converts 2nd field to numeric and prints it
awk '{print $2+0}' <<< 'edge +53.0°C foo bar'
53
# converts 2nd field to float with one decimal point and prints it
awk '{printf "%.1fn", $2+0}' <<< 'edge +53.0°C foo bar'
53.0
因此,对于您的情况,您可以使用:
sensors | awk '/edge/ {printf "%.1fn", $2+0}'
请您尝试以下操作。
awk 'match($2,/[0-9]+(.[0-9]+)?/){print substr($2,RSTART,RLENGTH)}' Input_file
OR
sensors | awk 'match($2,/[0-9]+(.[0-9]+)?/){print substr($2,RSTART,RLENGTH)}'
解释:添加以上详细解释。
awk ' ##Starting awk porgram from here.
match($2,/[0-9]+(.[0-9]+)?/){ ##using match function to match digits DOT digits(optional) in 2nd field.
print substr($2,RSTART,RLENGTH) ##printing sub string from 2nd field whose starting point is RSTART till RLENGTH.
}
' Input_file ##Mentioning Input_file name here.