You are not logged in.

#1 2010-02-09 21:23:32

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

Very simple bash question

I don't know how to use sed etc. so I thought someone who knows bash scripting can help me. The following is the command I execute and it's output:

$ aticonfig --adapter=0 --od-gettemperature

Adapter 0 - ATI Mobility Radeon HD 4300 Series
            Sensor 0: Temperature - 58.50 C

Now I need a script (preferably a one-liner) that will return 58.50 (the temperature) from the above output.

Any help is much appreciated.


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-02-09 21:36:36

xstaticxgpx
Member
Registered: 2008-10-22
Posts: 48

Re: Very simple bash question

aticonfig --adapter=0 --od-gettemperature | tail -n1 | gawk '{ printf $5 }'

Offline

#3 2010-02-09 21:39:33

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

Re: Very simple bash question

Great thank you!

If you don't mind, can you explain how does this work?


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-02-09 21:42:56

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: Very simple bash question

another way that should work

aticonfig --adapter=0 --od-gettemperature | egrep -o [0-9][0-9]\.[0-9][0-9]

Hofstadter's Law:
           It always takes longer than you expect, even when you take into account Hofstadter's Law.

Offline

#5 2010-02-09 21:47:11

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Very simple bash question

Don't mean to be awkward, but here's an alternative:

aticonfig --adapter=0 --od-gettemperature | awk '/^ +.*/ {print $5}'

Extra regex vs extra subshell. ymmv. smile

Edit: I hadn't seen Cyrusm's so my last line probably doesn't make sense. wink

Last edited by skanky (2010-02-09 21:51:46)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#6 2010-02-09 21:48:57

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

Re: Very simple bash question

Guys, they are all great and everything, but I still don't understand how any of them actually works. Care to explain? 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-02-09 21:50:24

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Very simple bash question

dcc24 wrote:

Great thank you!

If you don't mind, can you explain how does this work?

1) pipes via tail which returns the last line, and pipes that through gawk, which prints the 5th field - space separated.
2) Pipes via grep which returns the text anywhere in the output that matches "digitdigit.digitdigit"
3) Pipes via awk which returns the 5th field (space separated) of the line that starts with a space.

There are also other ways of doing it. wink


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#8 2010-02-09 21:52:55

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

Re: Very simple bash question

Great, thanks!


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

#9 2010-02-09 23:09:56

briest
Member
From: Katowice, PL
Registered: 2006-05-04
Posts: 468

Re: Very simple bash question

Hey, what about

aticonfig --adapter=0 --od-gettemperature | awk 'END{print $5}'

? No extra regex wink

Offline

#10 2010-02-10 03:33:50

majiq
Member
Registered: 2009-03-06
Posts: 259

Re: Very simple bash question

No need to stay away from sed though. Better to learn it eventually:

aticonfig --adapter=0 --od-gettemperature | sed -n -e '/Temperature/s/^.* - //;s/ C.*$//p'

What that says is take the output, don't automatically print the line [-n], then apply the following filter [-e]:
1. find the line with temperature (/Temperature/),
2. Remove everything up until the " - " [s/^.* - //]
3. Remove everything after the " C" [s/ C.*$//]
4. Print the resulting line [p]

Somewhat more painful, but it could be worse. :-)

Offline

#11 2010-02-10 10:54:23

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Very simple bash question

briest wrote:

Hey, what about

aticonfig --adapter=0 --od-gettemperature | awk 'END{print $5}'

? No extra regex wink

Nice one. I'd forgotten about that little foible. smile


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

Board footer

Powered by FluxBB