Results 1 to 9 of 9
Hello all, new to these forums.
I'm trying to make a little Superkaramba theme.. I've came along ways from last night. I started looking up functions like "cut", and "awk" ...
- 07-06-2008 #1Just Joined!
- Join Date
- Jul 2008
- Location
- Roanoke, VA
- Posts
- 13
Help with this little script..
Hello all, new to these forums.
I'm trying to make a little Superkaramba theme.. I've came along ways from last night. I started looking up functions like "cut", and "awk" - and reading tons of examples(many from this very forum, which is why I registered)
So, my question is:
"sensors" returns for me:
Trying to extract the first line, "Core0" - the 29C one.wasted@tronic:~$ sensors
k8temp-pci-00c3
Adapter: PCI adapter
Core0 Temp: +29.0°C
Core0 Temp: +22.0°C
Core1 Temp: +22.0°C
Core1 Temp: +19.0°C
Here's what I have:
How do I just solely return the 29? I was thinking about awk, and printing $1, but since there's two lines, it prints $1 from both lines.Code:wasted@tronic:~$ sensors | grep Core0 | cut -d'+' -f2 | cut -d'.' -f1 29 22
Thanks for your help
- 07-06-2008 #2Linux User
- Join Date
- Jun 2007
- Posts
- 318
If you want the 1st line found you could use the 'head' command.
EDIT:Code:sensors | grep Core0 | head -n1 | cut -d'+' -f2 | cut -d'.' -f1
Just figured this out in awk:
Code:sensors | awk '/Core0/ {print substr($3,2,2); exit}'
- 07-07-2008 #3Just Joined!
- Join Date
- Jul 2008
- Location
- Roanoke, VA
- Posts
- 13
Thanks! That';s exactly what I was looking for.
head -n1 is kind of useful. head -n2 shows both lines, though. I tried playing with your print substr, but failed(trying to retrieve the second value.)
i tried:
But it just returns spaces. I was thinking $6 would pick up the second temperature, since it's the 6th value.Code:sensors | awk '/Core0/ {print substr($6,2,2); exit}'
How could you retrieve the second value, though?
- 07-07-2008 #4I really don't understand what's going on in this thread, but I'll stick my neck out. To get just the second line, do this:head -n1 is kind of useful. head -n2 shows both lines, though. I tried playing with your print substr, but failed(trying to retrieve the second value.)
Code:blah blah | head -n2 | tail -n1
--
Bill
Old age and treachery will overcome youth and skill.
- 07-07-2008 #5Just Joined!
- Join Date
- Jul 2008
- Location
- Roanoke, VA
- Posts
- 13
Thanks. Much appreciation to both of you.
- 07-07-2008 #6Linux User
- Join Date
- Jun 2007
- Posts
- 318
The $6 means to use the 6th field of each line found. Since there are 3 (space separated) fields on each line that's why you got nothing. You'd have to add a counter to select the line you want.
Code:sensors | awk 'BEGIN {ct=0} /Core0/ {ct=ct+1; if (ct==2) {print substr($3,2,2); exit}}'
- 07-08-2008 #7Just Joined!
- Join Date
- Jul 2008
- Location
- Roanoke, VA
- Posts
- 13
Thank you. I'm saving this.. very useful stuff. I appreciate all the help so far. Truly badass.
- 07-11-2008 #8Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
To get the third line you don't really need a function to do it. awk has built-in support to extract line(s) by line number (1-based). That's why NR exists for a reason. The above code could be written as:
I don't use substr here because if the temperature is 3-digits (I know it's not likely but still possible) then you only get the first two-digits of the temperature.Code:sensors | awk '(NR==3),/Core0/{print $3}' | cut -f2 -d+ | cut -f1 -d.
- 07-12-2008 #9Linux User
- Join Date
- Aug 2006
- Posts
- 458
Code:awk 'BEGIN{FS="[a-z:+°C]+"}/Core0/{ print $4;exit}' file


Reply With Quote
