If you want to see an alternative, that uses regexp and works (Arjen's
version failed because your original spec didn't clearly indicate (to
us Tcl'ers) that CPUTIN: was a line prefix), this is also a
possibility:
proc getCPUTemp {} {
if {1 != [regexp -all -line ^CPUTIN:.*$ [exec sensors] answer} {
error "Did not get a single line from \[exec sensors\] output"
}
return $answer
}
Explaining:
The -line option to regexp causes the anchors ^ and $ to also anchor to
newlines as well as the start and ends of the string.
The -all causes regexp to search for all matches in the input (and
regexp's return value in this form [form without -inline option] is
documented to be a count of the number of matches found).
^CPUTIN:.*$ This finds lines that begin with CPUTIN: and the .*$
causes the regex engine to capture the remainer of the line as part of
the match. The ^ is the "start" anchor, in this case looking for the
start of lines, $ is the "end" anchor, in this case looking for the end of a
line.
answer is regexp's result variable, the item matched (technically in
this form the last item matched) will be stored here, if there are
any matches.
The 'if' simply checks regexp's return value (a count of the number of
matches found. If it is not 1, trigger an error.
Otherwise return contents of 'answer' (because it contains the matched
line, and there was only 1, so it is the line that is desired.