You are not logged in.

#1 2010-09-11 20:28:11

dcc24
Member
Registered: 2009-10-31
Posts: 732

Command line experts, I need your help!

Kernel >= 2.6.36.x supports temperature monitoring for ATI cards (HD4xxx or later) using the open source drivers. Great! Right? So, here is the sensors output:

$ sensors
radeon-pci-0200
Adapter: PCI adapter
temp1:       +69.0°C                                    

acpitz-virtual-0
Adapter: Virtual device
temp1:       +26.8°C  (crit = +100.0°C)                  
temp2:        +0.0°C  (crit = +100.0°C)                  

coretemp-isa-0000
Adapter: ISA adapter
Core 0:      +56.0°C  (high = +80.0°C, crit = +90.0°C)  

coretemp-isa-0001
Adapter: ISA adapter
Core 2:      +54.0°C  (high = +80.0°C, crit = +90.0°C)  

Basically, I need a one-liner that will:

- Find the line that starts with "radeon-pci",
- Go 2 lines below,
- Extract "69.0" from that line,
- Return it without a newline character at the end.

I'd really like to learn how to do this myself, so if you can also post how your script works, that'll be great. Thanks!

Last edited by dcc24 (2010-09-11 20:29:07)


It is better to keep your mouth shut and be thought a fool than to open it and remove all doubt. (Mark Twain)

My AUR packages

Offline

#2 2010-09-11 20:47:04

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Command line experts, I need your help!

I'm sure someone can do this more cleanly, but I'll bite:

sensors | sed -n '/radeon/,+2s|.*+\([[:digit:]\.]\+\).*|\1|p'

Last edited by falconindy (2010-09-11 20:52:45)

Offline

#3 2010-09-11 20:50:03

dcc24
Member
Registered: 2009-10-31
Posts: 732

Re: Command line experts, I need your help!

falconindy wrote:

I'm sure someone can do this more cleanly with only sed, but I'll bite:

sensors | grep -A2 "radeon" | sed -n 's/temp1:[[:blank:]]\++\([[:digit:]\.]\+\).*/\1/p'

This one has a lot of whitespace at the end and a newline character. I can't fix it since I have no idea how sed works smile


It is better to keep your mouth shut and be thought a fool than to open it and remove all doubt. (Mark Twain)

My AUR packages

Offline

#4 2010-09-11 20:52:57

olvar
Member
Registered: 2009-11-13
Posts: 97

Re: Command line experts, I need your help!

Probably this is quite bloated, but I would start with:

sensors | awk 'BEGIN {line = -1} {if ($0 ~ /radeon-pci-0200/) { line=NR+2;} if (NR == line) {print $2}}'

Of course you can also do

cut -d\  -f14  /proc/acpi/thermal_zone/THRM/temperature

But that feels like cheating tongue

Offline

#5 2010-09-11 20:53:15

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Command line experts, I need your help!

Perhaps you could describe the target application this is being used in...

My latest OCD edit doesn't show any white space after the end of the matched token:

$ sensors | sed -n '/radeon/,+2s@.*+\([[:digit:]\.]\+\).*@\1@p' | cat -At
69.0$

Last edited by falconindy (2010-09-11 20:55:15)

Offline

#6 2010-09-11 20:55:54

dcc24
Member
Registered: 2009-10-31
Posts: 732

Re: Command line experts, I need your help!

olvar wrote:
sensors | awk 'BEGIN {line = -1} {if ($0 ~ /radeon-pci-0200/) { line=NR+2;} if (NR == line) {print $2}}'

This doesn't strip the + sign, or the "degrees celcius" part, or the newline character smile

olvar wrote:

Of course you can also do

cut -d\  -f14  /proc/acpi/thermal_zone/THRM/temperature

But that feels like cheating tongue

I don't have a sysfs interface for radeon (i.e THRM doesn't exist) but nice try smile


It is better to keep your mouth shut and be thought a fool than to open it and remove all doubt. (Mark Twain)

My AUR packages

Offline

#7 2010-09-11 20:58:20

dcc24
Member
Registered: 2009-10-31
Posts: 732

Re: Command line experts, I need your help!

falconindy wrote:

Perhaps you could describe the target application this is being used in...

My latest OCD edit doesn't show any white space after the end of the matched token:

$ sensors | sed -n '/radeon/,+2s@.*+\([[:digit:]\.]\+\).*@\1@p' | cat -At
69.0$

It is to be used in conky among some personal logging scripts.
Now this version is almost perfect! I can think of adding another sed at the end to remove the "$" (that $ doesn't come from the bash prompt, it's a real character), but there should be a cleaner way, right?

Last edited by dcc24 (2010-09-11 20:59:39)


It is better to keep your mouth shut and be thought a fool than to open it and remove all doubt. (Mark Twain)

My AUR packages

Offline

#8 2010-09-11 20:59:41

olvar
Member
Registered: 2009-11-13
Posts: 97

Re: Command line experts, I need your help!

ups, forgot to delete the undesired characters:

sensors | awk 'BEGIN {line = -1} {if ($0 ~ /radeon-pci-0200/) { line=NR+2;} if (NR == line) {print substr($2,2,6)}}'

Edit:
That will work as long you don't go over 100 degrees. if you do, then well need to find the character instead of putting "6", and also find a way to cool your cpu tongue

Last edited by olvar (2010-09-11 21:01:46)

Offline

#9 2010-09-11 21:01:38

dcc24
Member
Registered: 2009-10-31
Posts: 732

Re: Command line experts, I need your help!

olvar wrote:

ups, forgot to delete the undesired characters:

sensors | awk 'BEGIN {line = -1} {if ($0 ~ /radeon-pci-0200/) { line=NR+2;} if (NR == line) {print substr($2,2,6)}}'

Make that "6" at the end a "4", we are all set smile


It is better to keep your mouth shut and be thought a fool than to open it and remove all doubt. (Mark Twain)

My AUR packages

Offline

#10 2010-09-11 21:11:43

codycarey
Member
Registered: 2009-08-21
Posts: 154

Re: Command line experts, I need your help!

whatever | sed '/^radeon-pci/,/^$/!d' | sed -n 's/^temp.*+\(.*\)°C.*$/\1/p'

This'll output only the numbers you want, it works with all temperatures, and will work if you have more than one temperature reading.

Last edited by codycarey (2010-09-11 21:13:34)

Offline

#11 2010-09-11 21:43:09

barto
Member
From: Budapest, Hungary
Registered: 2009-10-22
Posts: 88

Re: Command line experts, I need your help!

A dirty but reliable way to get rid of all newlines:

echo -n $(<command with newlines in its output>)

So my solution would be:

echo -n $(sensors | sed -n '/^radeon-pci/,+2 s/[^+]*+\([0-9.]*\)°C/\1/p')

Works for the acpitz, coretemp (or whatever) values too. smile


“First principle, Clarice. Simplicity” – Dr. Hannibal Lecter

Offline

#12 2010-09-11 23:03:56

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Command line experts, I need your help!

dcc24 wrote:
falconindy wrote:

Perhaps you could describe the target application this is being used in...

My latest OCD edit doesn't show any white space after the end of the matched token:

$ sensors | sed -n '/radeon/,+2s@.*+\([[:digit:]\.]\+\).*@\1@p' | cat -At
69.0$

It is to be used in conky among some personal logging scripts.
Now this version is almost perfect! I can think of adding another sed at the end to remove the "$" (that $ doesn't come from the bash prompt, it's a real character), but there should be a cleaner way, right?

The $ at the end is an EOL character generated by cat (the -A flag). I was merely showing you that there's no whitespace after the extracted expression.

Offline

#13 2010-09-12 02:53:15

jt512
Member
Registered: 2009-02-19
Posts: 262

Re: Command line experts, I need your help!

olvar wrote:
cut -d\  -f14  /proc/acpi/thermal_zone/THRM/temperature

But that feels like cheating tongue

If you want to cheat:

grep -o [[:digit:]]* /proc/acpi/thermal_zone/THRM/temperature

Offline

#14 2010-09-12 08:23:35

dcc24
Member
Registered: 2009-10-31
Posts: 732

Re: Command line experts, I need your help!

falconindy wrote:

The $ at the end is an EOL character generated by cat (the -A flag). I was merely showing you that there's no whitespace after the extracted expression.

I see that, but when I put your first version in conky, it generated a lot of whitespace after the expression. No idea why, though.


It is better to keep your mouth shut and be thought a fool than to open it and remove all doubt. (Mark Twain)

My AUR packages

Offline

#15 2010-09-14 06:35:56

quigybo
Member
Registered: 2009-01-15
Posts: 223

Re: Command line experts, I need your help!

barto wrote:

A dirty but reliable way to get rid of all newlines:

echo -n $(<command with newlines in its output>)
<command> | tr -d '\n'

Of course this will get rid of all newlines, not just trailing newlines.

Offline

Board footer

Powered by FluxBB