bash 中的正则表达式与 awk

Regular expression in bash with awk

我正在尝试从 bash 内的流明传感器获取我的 AMD GPU 温度。 所以我用管道 awk 得到正确的行。但是现在我需要一个正则表达式来从中获取数据。

我当前的代码是:

sensors | awk '/edge/ {print}'

这输出 +53.0°C

现在我只需要 53.0.我如何在 bash 中执行此操作?

无需任何正则表达式,您可以在 awk:

中执行此操作
# prints 2nd field from input
awk '{print }' <<< 'edge +53.0°C foo bar'
+53.0°C

# converts 2nd field to numeric and prints it
awk '{print +0}' <<< 'edge +53.0°C foo bar'
53

# converts 2nd field to float with one decimal point and prints it
awk '{printf "%.1f\n", +0}' <<< 'edge +53.0°C foo bar'
53.0

所以对于你的情况,你可以使用:

sensors | awk '/edge/ {printf "%.1f\n", +0}'

能否请您尝试以下。

awk 'match(,/[0-9]+(\.[0-9]+)?/){print substr(,RSTART,RLENGTH)}' Input_file

sensors | awk 'match(,/[0-9]+(\.[0-9]+)?/){print substr(,RSTART,RLENGTH)}'

说明:为以上添加详细说明。

awk '                                ##Starting awk porgram from here.
match(,/[0-9]+(\.[0-9]+)?/){       ##using match function to match digits DOT digits(optional) in 2nd field.
  print substr(,RSTART,RLENGTH)    ##printing sub string from 2nd field whose starting point is RSTART till RLENGTH.
}
' Input_file                         ##Mentioning Input_file name here.