0

I am using this to show my cpu temperature.

echo CPU Temperature >> /home/andy/bin/HD_AND_CPU_TEMPS.txt

sensors -f | grep "temp1" >> /home/andy/bin/HD_AND_CPU_TEMPS.txt
geany /home/andy/bin/HD_AND_CPU_TEMPS.txt

The radeon is my graphics CPU.

radeon-pci-0008
Adapter: PCI adapter
temp1:       +115.1°F  (crit = +248.0°F, hyst = +213.6°F)

k10temp-pci-00c3
Adapter: PCI adapter
temp1:        +82.4°F  (high = +158.0°F)
                       (crit = +169.5°F, hyst = +168.3°F)

I want the file to only show the 2nd temp.

But there are two temp1s?

How can show only the 2nd temp?

2 Answers 2

1

The following code prints the temp1 line only if it follows a line containing k10temp-pci-00c3:

$ sensors -f | awk '/k10temp-pci-00c3/{f=1} f && /temp1/{print; f=0}'
temp1:        +82.4°F  (high = +158.0°F)

If you also want the heading:

$ cat sensors-f | awk 'BEGIN{print"CPU Temperature"} /k10temp-pci-00c3/{f=1} f && /temp1/{print; f=0}'
CPU Temperature
temp1:        +82.4°F  (high = +158.0°F)

How it works

  • BEGIN{print"CPU Temperature"} prints the heading.

  • /k10temp-pci-00c3/{f=1} sets the awk variable f to 1 (true) when a line containing k10temp-pci-00c3 is found.

  • f && /temp1/{print; f=0} will print a line if f is true and the line contains temp1. This also sets f back to zero (false).

Alternative: using sed

$ sensors -f | sed -n '/k10temp-pci-00c3/,/temp1/{/temp1/p}'
temp1:        +82.4°F  (high = +158.0°F)
1

you can pass the chip name as argument sensors k10temp-pci-00c3 to let sensors software only read this specific sensor.

$ sensors k10temp-pci-00c3
Adapter: PCI adapter
temp1:        +82.4°F  (high = +158.0°F)
              (crit = +169.5°F, hyst = +168.3°F)

This will result in faster read compared to reading all of the available sensors and then limiting the output.

And then you would want to get only the temp1 line, simplest way is to use grep

$ sensors k10temp-pci-00c3 | grep temp1
temp1:        +82.4°F  (high = +158.0°F)

To get only the temperature: Use awk to treat that line as column and get the 2nd column

$ sensors k10temp-pci-00c3 | grep temp1 | awk '{print $2}'
+82.4°F

I believe this method works faster and use less processing power.

For other people with different machines, the sensor output could be the different, change what to grep accordingly. On my machine the sensor label is Tdie instead of temp1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .