Could you please try following.
echo "Core 0: +53.0°C (high = +80.0°C, crit = +100.0°C)" |
sed 's/[^+]*+\([^ ]*\).*/\1/'
OR in case you don't want °
in output then try following.
echo "Core 0: +53.0°C (high = +80.0°C, crit = +100.0°C)" |
sed 's/[^+]*+\([^°]*\).*/\1/'
Explanation of above code:
I have used sed
's capability of saving matched regex into temp buffer memory and given it a reference as \1
to print it later.
Following explanation is only for explaining purposes to run code use above code please.
sed ' ##Starting sed program here.
s ##Using s option for substitution method by sed on Lines of Input_file.
/[^+]*+ ##Using regex to have till 1st occurrence of + here.
\([^ ]*\) ##Using memory buffer saving capability of sed to save matched regex in it.
##Have used regex to match everything till occurrence of space. Which should catch 1st temperature.
.* ##Mentioning .* to match all rest line.
/\1/' ##Now substituting whole line with 1st buffer matched value.
Explanation of why OP's code not working: Since OP is using .*+
in his/her attempt which is a GREEDY match so hence it is matching the last occurrence of +
thus it is giving last temperature value.
Note: Added link provided by Sundeep sir in comments for Greedy match understanding.