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'
awk
,sed
,perl
, ...)."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.