LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 08-15-2025, 04:41 AM   #1
breadbin
Member
 
Registered: Sep 2002
Location: Dublin, Ireland
Distribution: Linux Mint 17
Posts: 222

Rep: Reputation: 28
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.
 
Old 08-15-2025, 04:44 AM   #2
viel
Member
 
Registered: Jul 2021
Location: Arcadia
Distribution: LFS, (Slackware-15.0 backup)
Posts: 736

Rep: Reputation: Disabled
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.
 
Old 08-15-2025, 04:50 AM   #3
breadbin
Member
 
Registered: Sep 2002
Location: Dublin, Ireland
Distribution: Linux Mint 17
Posts: 222

Original Poster
Rep: Reputation: 28
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)
 
Old 08-15-2025, 04:52 AM   #4
breadbin
Member
 
Registered: Sep 2002
Location: Dublin, Ireland
Distribution: Linux Mint 17
Posts: 222

Original Poster
Rep: Reputation: 28
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.
 
Old 08-15-2025, 04:52 AM   #5
viel
Member
 
Registered: Jul 2021
Location: Arcadia
Distribution: LFS, (Slackware-15.0 backup)
Posts: 736

Rep: Reputation: Disabled
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.
 
Old 08-15-2025, 05:11 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 26,460

Rep: Reputation: 8608Reputation: 8608Reputation: 8608Reputation: 8608Reputation: 8608Reputation: 8608Reputation: 8608Reputation: 8608Reputation: 8608Reputation: 8608Reputation: 8608
https://zzzcode.ai/code-refactor?id=...d-c682eacf3347
 
1 members found this post helpful.
Old 08-15-2025, 06:07 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,569

Rep: Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268
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.
Old 08-15-2025, 07:12 AM   #8
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 4,317

Rep: Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172

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?

 
Old 08-15-2025, 09:48 AM   #9
breadbin
Member
 
Registered: Sep 2002
Location: Dublin, Ireland
Distribution: Linux Mint 17
Posts: 222

Original Poster
Rep: Reputation: 28
Quote:
Originally Posted by pan64 View Post
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 View Post
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 View Post
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?
 
Old 08-15-2025, 02:31 PM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,110
Blog Entries: 1

Rep: Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343
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.
  


Reply

Tags
awk, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] New line separator for each grep result (Even if grep outupt is null) eng_mohammedmostafa Linux - Newbie 13 04-11-2020 01:07 PM
I'm not sure how this happened but I'm sure there is a lesson in there somewhere mreff555 Linux - Newbie 6 09-24-2012 02:09 AM
[SOLVED] Grep for the result of a command within the result of another command jasonws Programming 6 11-18-2010 02:39 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:16 PM.

Contact Us - Advertising Info - Rules - Privacy - Donations - Contributing Member - LQ Sitemap - "Weather apps tell you it'll rain. Wyndo tells you when to go."
Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions