Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-15-2025, 04:41 AM
|
#1
|
|
Member
Registered: Sep 2002
Location: Dublin, Ireland
Distribution: Linux Mint 17
Posts: 222
Rep:
|
using grep and sed - I'm sure there is an easier way to get to this result
I have finally got the result I was looking for, it only took me about an hour to get here! I am using the sensors command to show the computers temperatures and on this new computer there is loads of information. It is actually 47 lines long and finding the CPU and GPU temperatures isn't as easy as it could be. So I decided to dive into grep and sed, I have never really used them before, but I wanted to try to use them to get the result I wanted. Which is
Code:
CPU: 25.0°C
GPU: 25.0°C
or whatever they are. And after a long process I managed it but I think it is embarrassingly noob and wondering if there was anything else I could have done differently for the future. The command I finally came up with is
Code:
sensors | sed "s/Tctl:/CPU/" | sed "s/junction:/GPU\ /" | grep -e "CPU" -e "GPU" | sed "s/([^)]*)//g"
I had to use the sed commands first to change the names because it wouldn't work after the grep commands. But I never really used pipes either so it is good practice.
The hardest part for me was the sed part at the end and it works but I had a hard time understanding how until I went to the GNU regular expression page and it seems to make a bit more sense now. The "junction:/GPU\ /" bit is to format the output under each other because I presume the CPU had an extra tab inserted.
Not pretty but it works and I am hoping to somehow use the watch command to keep an eye on them and maybe in the future put the output in a widget or something. I'm sure there are already widgets that do this and do it better but it is about the learning for me.
|
|
|
|
08-15-2025, 04:44 AM
|
#2
|
|
Member
Registered: Jul 2021
Location: Arcadia
Distribution: LFS, (Slackware-15.0 backup)
Posts: 736
Rep: 
|
What is the full sort of sensors?
Code:
Adapter: ISA adapter
Package id 0: +88.0°C (high = +100.0°C, crit = +100.0°C)
Core 0: +87.0°C (high = +100.0°C, crit = +100.0°C)
Core 1: +88.0°C (high = +100.0°C, crit = +100.0°C)
Core 2: +77.0°C (high = +100.0°C, crit = +100.0°C)
Core 3: +79.0°C (high = +100.0°C, crit = +100.0°C)
iwlwifi_1-virtual-0
Adapter: Virtual device
temp1: N/A
acpitz-acpi-0
Adapter: ACPI interface
temp1: +85.0°C (crit = +103.0°C)
nouveau-pci-0100
Adapter: PCI adapter
GPU core: N/A (min = +0.60 V, max = +1.20 V)
temp1: N/A (high = +95.0°C, hyst = +3.0°C)
(crit = +105.0°C, hyst = +5.0°C)
(emerg = +135.0°C, hyst = +5.0°C)
asus-isa-0000
Adapter: ISA adapter
cpu_fan: 3200 RPM
temp1: +85.0°C
pch_skylake-virtual-0
Adapter: Virtual device
temp1: +57.5°C
BAT0-acpi-0
Adapter: ACPI interface
in0: 14.40 V
Last edited by viel; 08-15-2025 at 04:45 AM.
|
|
|
|
08-15-2025, 04:50 AM
|
#3
|
|
Member
Registered: Sep 2002
Location: Dublin, Ireland
Distribution: Linux Mint 17
Posts: 222
Original Poster
Rep:
|
spd5118-i2c-16-53
Adapter: SMBus PIIX4 adapter port 0 at 0b00
temp1: +32.8°C (low = +0.0°C, high = +55.0°C)
(crit low = +0.0°C, crit = +85.0°C)
k10temp-pci-00c3
Adapter: PCI adapter
Tctl: +43.5°C
Tccd1: +33.2°C
amdgpu-pci-0300
Adapter: PCI adapter
vddgfx: 91.00 mV
fan1: 0 RPM (min = 0 RPM, max = 6000 RPM)
edge: +31.0°C (crit = +100.0°C, hyst = -273.1°C)
(emerg = +105.0°C)
junction: +32.0°C (crit = +110.0°C, hyst = -273.1°C)
(emerg = +115.0°C)
mem: +30.0°C (crit = +105.0°C, hyst = -273.1°C)
(emerg = +110.0°C)
PPT: 4.00 W (cap = 170.00 W)
nvme-pci-0f00
Adapter: PCI adapter
Composite: +36.9°C (low = -273.1°C, high = +74.8°C)
(crit = +79.8°C)
Sensor 2: +51.9°C (low = -273.1°C, high = +65261.8°C)
iwlwifi_1-virtual-0
Adapter: Virtual device
temp1: +25.0°C
spd5118-i2c-16-51
Adapter: SMBus PIIX4 adapter port 0 at 0b00
temp1: +32.2°C (low = +0.0°C, high = +55.0°C)
(crit low = +0.0°C, crit = +85.0°C)
amdgpu-pci-1000
Adapter: PCI adapter
vddgfx: 1.19 V
vddnb: 1.01 V
edge: +40.0°C
PPT: 28.06 W
r8169_0_800:00-mdio-0
Adapter: MDIO adapter
temp1: +32.0°C (high = +120.0°C)
|
|
|
|
08-15-2025, 04:52 AM
|
#4
|
|
Member
Registered: Sep 2002
Location: Dublin, Ireland
Distribution: Linux Mint 17
Posts: 222
Original Poster
Rep:
|
I am guessing the k10temp is the cpu and the first amdgpu is the gpu. Not sure why there are 2 amdgpu sections but it is a lot to look at.
|
|
|
|
08-15-2025, 04:52 AM
|
#5
|
|
Member
Registered: Jul 2021
Location: Arcadia
Distribution: LFS, (Slackware-15.0 backup)
Posts: 736
Rep: 
|
Well you need to put it like code to see spaces ... to I can process it fine. 
Last edited by viel; 08-15-2025 at 05:12 AM.
|
|
|
|
08-15-2025, 05:11 AM
|
#6
|
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 26,460
|
|
|
|
1 members found this post helpful.
|
08-15-2025, 06:07 AM
|
#7
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,569
|
Field based processing is way better handled in something like awk - or perl.
If you don't know either, sed can handle all this in one invocation - there is never any need to pipe to grep, sed has the logic primitives to do it all. A little (lot) more arcane than awk, but if you don't want to learn yet another tool, sed will suffice. Concentrate on the data you want, not the other fluff. Something like this should work (untested).
Code:
sed -nE "/Tctl:/ {s/.+(\+.+C).*/CPU:\t\1/p} ; /junction:/ {s/.+(\+.+C).+/GPU:\t\1/p}"
|
|
|
1 members found this post helpful.
|
08-15-2025, 07:12 AM
|
#8
|
|
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 4,317
|
What version of sensors? Does it have the new -J flag for useful JSON output (which can then be parsed with a single jq call?
|
|
|
|
08-15-2025, 09:48 AM
|
#9
|
|
Member
Registered: Sep 2002
Location: Dublin, Ireland
Distribution: Linux Mint 17
Posts: 222
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
|
Thanks, I thought there might be something like this available. I'm sure there is an AI thing that can write the whole thing too from scratch.
Quote:
Originally Posted by syg00
Field based processing is way better handled in something like awk - or perl.
If you don't know either, sed can handle all this in one invocation - there is never any need to pipe to grep, sed has the logic primitives to do it all. A little (lot) more arcane than awk, but if you don't want to learn yet another tool, sed will suffice. Concentrate on the data you want, not the other fluff. Something like this should work (untested).
Code:
sed -nE "/Tctl:/ {s/.+(\+.+C).*/CPU:\t\1/p} ; /junction:/ {s/.+(\+.+C).+/GPU:\t\1/p}"
|
I don't have a problem using other tools, maybe I would if I knew more about any of them, I thought sed was probably best for it. It looks like the sort of job it was created for
Quote:
Originally Posted by boughtonp
What version of sensors? Does it have the new -J flag for useful JSON output (which can then be parsed with a single jq call?
|
It is version 3.6.0 so yes it does have the json output, would that be easier to work with?
|
|
|
|
08-15-2025, 02:31 PM
|
#10
|
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,110
|
As others said, awk is best here:
Code:
sensors | awk '$1=="Tctl:" {print "CPU:",$2} $1=="junction:" {print "GPU:",$2}'
Last edited by MadeInGermany; 08-15-2025 at 02:32 PM.
|
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 06:16 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|