Rich <ri...@example.invalid> writes:
> Cecil Westerhof <
Ce...@decebal.nl> wrote:
>> Rich <ri...@example.invalid> writes:
>>
>>> Cecil Westerhof <
Ce...@decebal.nl> wrote:
>>>> I want to fetch the frequencies out of /proc/cpuinfo.
>>>>
>>>> This information is in line like:
>>>> cpu MHz : 1400.000
>>>>
>>>> What is the best way to retrieve this information?
>>>
>>> "Best" is somewhat of an 'opinion' type item.
>>>
>>> One way to do so would be to read the file line by line, pattern
>>> matching each line against "cpu MHz", then when a match is found,
>>> extract the numerical value.
>>
>> That is what I do at the moment:
> [long code block elided - see prior post for details]
>>
>> But it seems a bit verbose. I was wondering if there was a way to
>> have an expression and a filename and return an array of matches.
>
> Install tcllib, then you can have a tcl level 'grep' where you pass it
> a pattern and a set of filenames.
I will look into that. But that would make my code depending on an
extra library?
>> Also: I would like to use something like:
>> set pattern "^cpu MHz.\S+: [0-9.]$"
>> instead of:
>> set pattern "^cpu MHz."
>>
>> But that does not work. Even:
>> set pattern "^cpu MHz.\S+"
>> does not work.
>>
>> What am I doing wrong?
>
> You forgot a + on your bracket expression, so it only matches a single
> character.
That was not very smart. :'-(
> The \S escape is "not whitespace" - you want \s
>
> $ rlwrap tclsh
> % set line "cpu MHz : 1400.000"
> cpu MHz : 1400.000
> % regexp {^cpu MHz.\s+: [0-9.]$} $line -> freq
> 0
> % regexp {^cpu MHz.\s+: [0-9.]+$} $line -> freq
> 1
> % regexp {^cpu MHz.\s+: [0-9.]+$} $line
> 1
>
> But you can also do more in the regexp. Change it to this one:
>
> ^cpu MHz\s+:\s([0-9.]+)$
I did it a bit different:
set pattern {^cpu MHz\s+: ([0-9]+)\.0+$}
There should be exactly one space after the ':'.
I only want the integral part.
After the point there are only zeros.
> And call the regexp command this way:
>
> % regexp {^cpu MHz\s+:\s([0-9.]+)$} $line -> freq
> 1
> % set freq
> 1400.000
> %
>
> And the regex engine also returns the frequency already extracted for
> you. The parenthesis do the extraction.
I thought something like this should be possible. ;-)
I now have:
while {[gets ${fid} line] != -1} {
if {[regexp ${pattern} ${line} -> frequency] == 1} {
incr count
if {${count} == 1} {
set max ${frequency}
.
.
.
And my format string is of-course now:
"total: %d\nmin: %d\nmax: %d\navg: %d\n"