Find the answer to your Linux question:
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" ...
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Location
    Roanoke, VA
    Posts
    13

    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:

    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

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    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}'

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Location
    Roanoke, VA
    Posts
    13
    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?

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  5. #5
    Just Joined!
    Join Date
    Jul 2008
    Location
    Roanoke, VA
    Posts
    13
    Thanks. Much appreciation to both of you.

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    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}}'

  7. #7
    Just 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.

  8. #8
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    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.

  9. #9
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk 'BEGIN{FS="[a-z:+°C]+"}/Core0/{ print $4;exit}' file

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...