Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Help with this little script..
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 07-06-2008   #1 (permalink)
wastedfluid
Just Joined!
 
Join Date: Jul 2008
Posts: 4
Question 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:

Quote:
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
Trying to extract the first line, "Core0" - the 29C one.

Here's what I have:

Code:
wasted@tronic:~$ sensors | grep Core0 | cut -d'+' -f2 | cut -d'.' -f1
29
22
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.

Thanks for your help
wastedfluid is offline   Reply With Quote
Old 07-06-2008   #2 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 207
If you want the 1st line found you could use the 'head' command.

Code:
sensors | grep Core0 | head -n1 | cut -d'+' -f2 | cut -d'.' -f1
EDIT:

Just figured this out in awk:
Code:
sensors | awk '/Core0/ {print substr($3,2,2); exit}'
vsemaska is offline   Reply With Quote
Old 07-07-2008   #3 (permalink)
wastedfluid
Just Joined!
 
Join Date: Jul 2008
Posts: 4
Quote:
Originally Posted by vsemaska View Post
If you want the 1st line found you could use the 'head' command.

Code:
sensors | grep Core0 | head -n1 | cut -d'+' -f2 | cut -d'.' -f1
EDIT:

Just figured this out in awk:
Code:
sensors | awk '/Core0/ {print substr($3,2,2); exit}'
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:

Code:
sensors | awk '/Core0/ {print substr($6,2,2); exit}'
But it just returns spaces. I was thinking $6 would pick up the second temperature, since it's the 6th value.

How could you retrieve the second value, though?
wastedfluid is offline   Reply With Quote
Old 07-07-2008   #4 (permalink)
wje_lf
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 969
Quote:
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 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:
Code:
blah blah | head -n2 | tail -n1
__________________
--
Bill

Old age and treachery will overcome youth and skill.
wje_lf is offline   Reply With Quote
Old 07-07-2008   #5 (permalink)
wastedfluid
Just Joined!
 
Join Date: Jul 2008
Posts: 4
Thanks. Much appreciation to both of you.
wastedfluid is offline   Reply With Quote
Old 07-07-2008   #6 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 207
Quote:
Originally Posted by wastedfluid View Post
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:

Code:
sensors | awk '/Core0/ {print substr($6,2,2); exit}'
But it just returns spaces. I was thinking $6 would pick up the second temperature, since it's the 6th value.

How could you retrieve the second value, though?
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}}'
vsemaska is offline   Reply With Quote
Old 07-08-2008   #7 (permalink)
wastedfluid
Just Joined!
 
Join Date: Jul 2008
Posts: 4
Thank you. I'm saving this.. very useful stuff. I appreciate all the help so far. Truly badass.
wastedfluid is offline   Reply With Quote
Old 07-11-2008   #8 (permalink)
secondmouse
Just Joined!
 
Join Date: May 2008
Location: NYC, moved from KS, MO
Posts: 42
Quote:
Originally Posted by vsemaska View Post

Code:
sensors | awk 'BEGIN {ct=0} /Core0/ {ct=ct+1; if (ct==2) {print substr($3,2,2); exit}}'
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:
Code:
sensors | awk '(NR==3),/Core0/{print $3}' | cut -f2 -d+ | cut -f1 -d.
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.
secondmouse is offline   Reply With Quote
Old 07-12-2008   #9 (permalink)
ghostdog74
Linux User
 
Join Date: Aug 2006
Posts: 327
Code:
awk 'BEGIN{FS="[a-z:+°C]+"}/Core0/{ print $4;exit}' file
ghostdog74 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 10:54 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0