0

I want to extract the maximum temperature value from the output of a command, which I store in a shell variable as follows:

res="$(get temperature all)"

where the result looks like

 Device 0 avs data:
    Temperature: 33.5 C
    voltage1V0:  0.926 V
    voltage1V8:  1.782 V
    voltage3V3:  3.265 V

 Device 1 avs data:
    Temperature: 32.6 C
    voltage1V0:  0.923 V
    voltage1V8:  1.780 V
    voltage3V3:  3.265 V

 Device 2 avs data:
    Temperature: 33.1 C
    voltage1V0:  0.920 V
    voltage1V8:  1.785 V
    voltage3V3:  3.290 V

 Device 3 avs data:
    Temperature: 33.1 C
    voltage1V0:  0.921 V
    voltage1V8:  1.780 V
    voltage3V3:  3.265 V

I want to extract the numerical value enclosed between the strings Temperature: and C, and find the maximum value from all the device records. For the above case, the output should be

33.5

since the temperature values are 33.5, 32.6, 33.1, 33.1

In the above command, the output gives

  • sometimes only Device 0 information,
  • sometimes Device 0, Device 1 information
  • sometimes Device 0, Device 1, Device 3, Device 4 information

so finding max temperature logic should be generic irrespective of devices count (though the max count is 4, min count is 1 for available devices).

So for example, if only a single device is present then the max temperature should be equal to device 0 temperature, otherwise we need to compare and print.

Edit 1 - I need to use sed or awk as grep -P option is not available.

echo 'Here is a string, and Here is another string.' | grep -oP '(?<=Here).*(?=string)'

grep: invalid option -- 'P'
  • Welcome to the site. Please indicate what you have already tried, in order to avoid that contributors trying to help point you in a direction which you already know doesn't work. When editing your question to do so, please indicate which tools you have at your disposal (awk, sed, perl, ...). – AdminBee Jul 27 '20 at 11:29
  • @AdminBee I tried to do it using grep like this but "echo 'Here is a string, and Here is another string.' | grep -oP '(?<=Here).*(?=string)' grep: invalid option -- 'P'" . So looks like -p is not available on my system. so need to use awk or sed instead. – Neha P Jul 27 '20 at 11:38
5

Assuming every device can contain only one "Temperature" reading, and you are only interested in printing the maximum (and not the associated device number), you can try the following:

awk '$1=="Temperature:" {if ($2>max) max=$2} END{printf("Maximum: %.1f\n",max)}'

This will parse all lines where the first space-separated field ($1) is Temperature:, and inspect the second field ($2, which is the numerical temperature value). If that is larger than the maximum so far, it is stored in the variable max (which behaves as if it were initially 0).

At the end, this maximum found is printed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.