grep help? - Trying to extra sensors data

Discussion in 'NZ Computing' started by The Hobbit, Sep 13, 2004.

  1. The Hobbit

    The Hobbit Guest

    Hi All,
    I'm trying to extract some data from my sensors output which looks like the
    following:
    --------------------------------------------------
    $ sensors

    asb100-i2c-0-2d
    Adapter: SMBus Via Pro adapter at e800
    VCore 1: +1.76 V (min = +1.76 V, max = +1.94 V) ALARM
    +3.3V: +3.34 V (min = +3.14 V, max = +3.47 V)
    +5V: +4.95 V (min = +4.76 V, max = +5.24 V)
    +12V: +12.16 V (min = +10.82 V, max = +13.19 V)
    -12V (reserved):
    -12.70 V (min = -0.00 V, max = -0.00 V)
    -5V (reserved):
    -5.33 V (min = -0.00 V, max = -0.00 V)
    CPU Fan: 5113 RPM (min = 1997 RPM, div = 4)
    Chassis Fan:
    3125 RPM (min = 3994 RPM, div = 2)
    Power Fan:2860 RPM (min = 3994 RPM, div = 2)
    M/B Temp: +29°C (high = +45°C, hyst = +40°C)
    CPU Temp (Intel):
    +46°C (high = +60°C, hyst = +50°C)
    Power Temp:
    +14°C (high = +45°C, hyst = +40°C)
    CPU Temp (AMD):
    +51°C (high = +60°C, hyst = +50°C)
    vid: +1.850 V
    alarms:

    $
    --------------------------------------------------

    I can extract things like the VCore by using:

    sensors | grep "VCore 1: " | cut -d"+" -f2 | cut -d"V" -f1

    and for CPU Fan:

    sensors | grep "CPU Fan" | cut -d":" -f2 | cut -d"R" -f1

    But, for things such as the Chassis Fan and the CPU Temp where the output
    runs over a couple of lines, I'm not getting any results.

    e.g.
    sensors | grep "Power Temp:" | cut -d"+" -f2 | cut -d"Â" -f1

    will return "Power Temp:" and not "14" as I'd expect.



    I'm planning to drop this info into RRDTool so any pointers to somewhere
    that I can learn what I'm doing wrong and/or something (please not NRG (at
    this point)) which coulds help me parse this info would be much appreciated.



    I'm running FC2 on a ASUSTeK A7V333-RAID (VIA KT333 Chipset) if that helps
    anyone.



    Cheers!
     
    The Hobbit, Sep 13, 2004
    #1
    1. Advertising

  2. The Hobbit

    AD. Guest

    On Mon, 13 Sep 2004 16:20:18 +1200, The Hobbit wrote:

    > But, for things such as the Chassis Fan and the CPU Temp where the output
    > runs over a couple of lines, I'm not getting any results.


    grep will only match inside a line - I don't think there is anything you
    can do about that. I think your best best is to somehow stop the lines
    wrapping in the first place.

    Or did I misunderstand? :)

    Cheers
    Anton
     
    AD., Sep 13, 2004
    #2
    1. Advertising

  3. The Hobbit

    Genius Guest

    The Hobbit wrote:

    > Hi All,
    > I'm trying to extract some data from my sensors output which looks like
    > the following:
    > --------------------------------------------------
    > $ sensors
    >
    > asb100-i2c-0-2d
    > Adapter: SMBus Via Pro adapter at e800
    > VCore 1: +1.76 V (min = +1.76 V, max = +1.94 V) ALARM
    > +3.3V: +3.34 V (min = +3.14 V, max = +3.47 V)
    > +5V: +4.95 V (min = +4.76 V, max = +5.24 V)
    > +12V: +12.16 V (min = +10.82 V, max = +13.19 V)
    > -12V (reserved):
    > -12.70 V (min = -0.00 V, max = -0.00 V)
    > -5V (reserved):
    > -5.33 V (min = -0.00 V, max = -0.00 V)
    > CPU Fan: 5113 RPM (min = 1997 RPM, div = 4)
    > Chassis Fan:
    > 3125 RPM (min = 3994 RPM, div = 2)
    > Power Fan:2860 RPM (min = 3994 RPM, div = 2)
    > M/B Temp: +29°C (high = +45°C, hyst = +40°C)
    > CPU Temp (Intel):
    > +46°C (high = +60°C, hyst = +50°C)
    > Power Temp:
    > +14°C (high = +45°C, hyst = +40°C)
    > CPU Temp (AMD):
    > +51°C (high = +60°C, hyst = +50°C)
    > vid: +1.850 V
    > alarms:
    >
    > $
    > --------------------------------------------------
    >
    > I can extract things like the VCore by using:
    >
    > sensors | grep "VCore 1: " | cut -d"+" -f2 | cut -d"V" -f1
    >
    > and for CPU Fan:
    >
    > sensors | grep "CPU Fan" | cut -d":" -f2 | cut -d"R" -f1
    >
    > But, for things such as the Chassis Fan and the CPU Temp where the output
    > runs over a couple of lines, I'm not getting any results.
    >
    > e.g.
    > sensors | grep "Power Temp:" | cut -d"+" -f2 | cut -d"Â" -f1
    >
    > will return "Power Temp:" and not "14" as I'd expect.
    >
    >
    >
    > I'm planning to drop this info into RRDTool so any pointers to somewhere
    > that I can learn what I'm doing wrong and/or something (please not NRG (at
    > this point)) which coulds help me parse this info would be much
    > appreciated.
    >
    >
    >
    > I'm running FC2 on a ASUSTeK A7V333-RAID (VIA KT333 Chipset) if that helps
    > anyone.
    >


    try preprocessing like this:

    sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="Chassis Fan"{print $2}'

    when sed sees line ending in : it just reads next line, then substitutes the
    embedded newline for nothing. the braces group commands together.
    you might find awk more useful than cut for some things because
    awk has handy substr function too.
     
    Genius, Sep 13, 2004
    #3
  4. The Hobbit

    The Hobbit Guest

    "Genius" <> wrote in message
    news:6639758.UWrncvuQV6@genius...
    > The Hobbit wrote:
    >
    > > Hi All,
    > > I'm trying to extract some data from my sensors output which looks like
    > > the following:
    > > --------------------------------------------------
    > > $ sensors
    > >
    > > asb100-i2c-0-2d
    > > Adapter: SMBus Via Pro adapter at e800

    <Snippage>
    > > M/B Temp: +29°C (high = +45°C, hyst = +40°C)
    > > CPU Temp (Intel):
    > > +46°C (high = +60°C, hyst = +50°C)
    > > Power Temp:
    > > +14°C (high = +45°C, hyst = +40°C)
    > > CPU Temp (AMD):
    > > +51°C (high = +60°C, hyst = +50°C)
    > > vid: +1.850 V
    > > alarms:
    > >
    > > $
    > > --------------------------------------------------

    <Snippage>
    >
    > try preprocessing like this:
    >
    > sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="Chassis Fan"{print

    $2}'
    >
    > when sed sees line ending in : it just reads next line, then substitutes

    the
    > embedded newline for nothing. the braces group commands together.
    > you might find awk more useful than cut for some things because
    > awk has handy substr function too.
    >

    Awesome - thanks for that genius, I'm much of the way there, now I'm just
    trying to get around some weirdness with the special characters which
    accompany the degrees symbol.

    My current temperature extraction command is as follows (probably HORRIBLY
    inefficient, but I've got to go join the wife i nfront of the telly so
    haven't had too much time to figure out what to do to tidy it).
    Aaaanyway...:

    $ sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="CPU Temp (AMD)"{print $2}'
    | cut -d"+" -f2 | sed -e 's/°C//' | cut -d"(" -f1 | sed 's/^[ \t]*//;s/[
    \t]*$//'


    Gives me a nice tidy '50'


    And even:
    $ CPU_Temp=`sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="CPU Temp
    (AMD)"{print $2}' | cut -d "+" -f2 | sed -e 's/°C//' | cut -d "(" -f1 | sed
    s/^[ \t]*//;s/[ \t]*$//'`
    $ echo $CPU_Temp
    50

    But when I copy and paste this exact command into my shell script:
    CPU_Temp=`sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="CPU Temp
    (AMD)"{print $2}' | cut -d"+" -f2 | sed -e 's/°C//' | cut -d"(" -f1 | sed
    s/^[ \t]*//;s/[ \t*$//'`

    It loses some of the spaces around the commands and the whole thing comes
    crumbling down :(

    Anyway - duty calls, must be off to spend some quality time with the wife
    before she put a hammer through all of my PCs.

    If anyone can help out it'd be most appreciated :)
     
    The Hobbit, Sep 15, 2004
    #4
  5. The Hobbit

    The Hobbit Guest

    "AD." <> wrote in message
    news:p...
    > On Mon, 13 Sep 2004 16:20:18 +1200, The Hobbit wrote:
    >
    > > But, for things such as the Chassis Fan and the CPU Temp where the

    output
    > > runs over a couple of lines, I'm not getting any results.

    >
    > grep will only match inside a line - I don't think there is anything you
    > can do about that. I think your best best is to somehow stop the lines
    > wrapping in the first place.


    Not sure how to stop these line from wrapping, it's just "what happens" when
    I run 'sensors' from the command line.. I've been looking into SED as
    suggested by 'genius' and have it working for the most part, just need to
    figure out how to trim off the special characters that accompany the degrees
    symbol :(
     
    The Hobbit, Sep 15, 2004
    #5
  6. The Hobbit wrote:
    > Anyway - duty calls, must be off to spend some quality time with the wife
    > before she put a hammer through all of my PCs.
    > If anyone can help out it'd be most appreciated :)


    Im not into smashing PC's but I'll tell my wife that your looking for
    someone to help out.

    :)
     
    Dave - Dave.net.nz, Sep 15, 2004
    #6
  7. The Hobbit

    The Hobbit Guest

    "Dave - Dave.net.nz" <Dave@_no_spam_here_please_dave.net.nz> wrote in
    message news:...
    > The Hobbit wrote:
    > > Anyway - duty calls, must be off to spend some quality time with the

    wife
    > > before she put a hammer through all of my PCs.
    > > If anyone can help out it'd be most appreciated :)

    >
    > Im not into smashing PC's but I'll tell my wife that your looking for
    > someone to help out.


    Doh! - bloody sentence structuring... ;)
     
    The Hobbit, Sep 15, 2004
    #7
  8. The Hobbit

    Harry. Guest

    The Hobbit wrote:

    >
    > "Genius" <> wrote in message
    > news:6639758.UWrncvuQV6@genius...
    >> The Hobbit wrote:
    >>
    >> > Hi All,
    >> > I'm trying to extract some data from my sensors output which looks like
    >> > the following:
    >> > --------------------------------------------------
    >> > $ sensors
    >> >
    >> > asb100-i2c-0-2d
    >> > Adapter: SMBus Via Pro adapter at e800

    > <Snippage>
    >> > M/B Temp: +29°C (high = +45°C, hyst = +40°C)
    >> > CPU Temp (Intel):
    >> > +46°C (high = +60°C, hyst = +50°C)
    >> > Power Temp:
    >> > +14°C (high = +45°C, hyst = +40°C)
    >> > CPU Temp (AMD):
    >> > +51°C (high = +60°C, hyst = +50°C)
    >> > vid: +1.850 V
    >> > alarms:
    >> >
    >> > $
    >> > --------------------------------------------------

    > <Snippage>
    >>
    >> try preprocessing like this:
    >>
    >> sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="Chassis
    >> Fan"{print

    > $2}'
    >>
    >> when sed sees line ending in : it just reads next line, then substitutes

    > the
    >> embedded newline for nothing. the braces group commands together.
    >> you might find awk more useful than cut for some things because
    >> awk has handy substr function too.
    >>

    > Awesome - thanks for that genius, I'm much of the way there, now I'm just
    > trying to get around some weirdness with the special characters which
    > accompany the degrees symbol.
    >
    > My current temperature extraction command is as follows (probably HORRIBLY
    > inefficient, but I've got to go join the wife i nfront of the telly so
    > haven't had too much time to figure out what to do to tidy it).
    > Aaaanyway...:
    >
    > $ sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="CPU Temp (AMD)"{print
    > $2}'
    > | cut -d"+" -f2 | sed -e 's/°C//' | cut -d"(" -f1 | sed 's/^[ \t]*//;s/[
    > \t]*$//'
    >
    >
    > Gives me a nice tidy '50'
    >
    >
    > And even:
    > $ CPU_Temp=`sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="CPU Temp
    > (AMD)"{print $2}' | cut -d "+" -f2 | sed -e 's/°C//' | cut -d "(" -f1 |
    > sed s/^[ \t]*//;s/[ \t]*$//'`
    > $ echo $CPU_Temp
    > 50
    >
    > But when I copy and paste this exact command into my shell script:
    > CPU_Temp=`sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="CPU Temp
    > (AMD)"{print $2}' | cut -d"+" -f2 | sed -e 's/°C//' | cut -d"(" -f1 | sed
    > s/^[ \t]*//;s/[ \t*$//'`
    >
    > It loses some of the spaces around the commands and the whole thing comes
    > crumbling down :(
    >
    > Anyway - duty calls, must be off to spend some quality time with the wife
    > before she put a hammer through all of my PCs.
    >
    > If anyone can help out it'd be most appreciated :)


    What a mess!

    Try this instead:

    sed '/:$/{N;s/\n//;}' j | awk -F: '$1=="CPU Temp (AMD)"{
    n=split($2,v,/ +/);print substr(v[2],0,length(v[2])-3)}'

    Or:

    sed '/:$/{N;s/\n//;}' j | perl -e '
    while (<>)
    {
    next unless $_ =~ m{^CPU Temp \(AMD\):\s+\+(\d+)..C\s+};
    print "$1\n";
    }
    '

    When you copy into a script you might lose some quotes - make sure
    they are all there and watch for newlines in wrong place.

    You could also do the line joining in perl instead of sed.
     
    Harry., Sep 15, 2004
    #8
  9. The Hobbit

    The Hobbit Guest

    Re: grep help? - Trying to extra sensors data - SOLVED

    "The Hobbit" <> wrote in message
    news:hJU1d.2714$...
    >
    > "Genius" <> wrote
    > > The Hobbit wrote:
    > >
    > > > Hi All,
    > > > I'm trying to extract some data from my sensors output which looks

    like
    > > > the following:
    > > > --------------------------------------------------
    > > > $ sensors
    > > >
    > > > asb100-i2c-0-2d
    > > > Adapter: SMBus Via Pro adapter at e800

    > <Snippage>
    > > > M/B Temp: +29°C (high = +45°C, hyst = +40°C)

    <Snippy Snip>
    > > > CPU Temp (AMD):
    > > > +51°C (high = +60°C, hyst = +50°C)

    <Snip>
    > > > $
    > > > --------------------------------------------------

    > <Snippage>
    > >
    > > try preprocessing like this:
    > >
    > > sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="Chassis

    Fan"{print
    > $2}'
    > >
    > > when sed sees line ending in : it just reads next line, then substitutes

    > the
    > > embedded newline for nothing. the braces group commands together.
    > > you might find awk more useful than cut for some things because
    > > awk has handy substr function too.
    > >

    > Awesome - thanks for that genius, I'm much of the way there, now I'm just
    > trying to get around some weirdness with the special characters which
    > accompany the degrees symbol.
    >
    > My current temperature extraction command is as follows (probably HORRIBLY
    > inefficient, <Snip> Aaaanyway...:
    >
    > $ sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="CPU Temp (AMD)"{print

    $2}'
    > | cut -d"+" -f2 | sed -e 's/°C//' | cut -d"(" -f1 | sed 's/^[ \t]*//;s/[
    > \t]*$//'
    >
    > Gives me a nice tidy '50'
    >
    > And even:
    > $ CPU_Temp=`sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="CPU Temp
    > (AMD)"{print $2}' | cut -d "+" -f2 | sed -e 's/°C//' | cut -d "(" -f1 |

    sed
    > s/^[ \t]*//;s/[ \t]*$//'`
    > $ echo $CPU_Temp
    > 50
    >
    > But when I copy and paste this exact command into my shell script:
    > CPU_Temp=`sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="CPU Temp
    > (AMD)"{print $2}' | cut -d"+" -f2 | sed -e 's/°C//' | cut -d"(" -f1 | sed
    > s/^[ \t]*//;s/[ \t*$//'`
    >
    > It loses some of the spaces around the commands and the whole thing comes
    > crumbling down :(


    Just thought I'd reply back to for the benifit of the archives, I've managed
    to resolve this by using AWK.

    For the reading that run over 2 lines
    CPU_Temp="`sensors | awk '{if (match($0, "AMD") ) {getline; printf("%d",
    $1);} }'`"

    echo $CPU_Temp


    And for one liners...
    MB_Temp="`sensors | awk '{if (match($0, "M/B Temp") ) {printf("%d",
    $3);} }'`"

    echo $MB_Temp


    Which basically works by Awk is being told to search each line for a 'match'
    to the enclosed string "AMD". When a match is found it retrieves the next
    line (getline) from which it prints the first integer (%d) of the first word
    ($1) on that line.

    Hope that helps someone in the future, and thanks to those who responded to
    this thread (yes even you Dave)
     
    The Hobbit, Sep 16, 2004
    #9
  10. The Hobbit

    The Hobbit Guest

    "Harry." <> wrote in message
    news:Rl32d.2878$...
    > The Hobbit wrote:
    >

    <Snip>
    > > My current temperature extraction command is as follows (probably

    HORRIBLY
    > > inefficient, but I've got to go join the wife i nfront of the telly so
    > > haven't had too much time to figure out what to do to tidy it).
    > > Aaaanyway...:
    > >
    > > $ sensors | sed '/:$/{N;s/\n//;}' | awk -F: '$1=="CPU Temp (AMD)"{print
    > > $2}'
    > > | cut -d"+" -f2 | sed -e 's/°C//' | cut -d"(" -f1 | sed 's/^[

    \t]*//;s/[
    > > \t]*$//'
    > >
    > >
    > > Gives me a nice tidy '50'
    > >

    <Snip>
    > > If anyone can help out it'd be most appreciated :)

    >
    > What a mess!
    >
    > Try this instead:
    >
    > sed '/:$/{N;s/\n//;}' j | awk -F: '$1=="CPU Temp (AMD)"{
    > n=split($2,v,/ +/);print substr(v[2],0,length(v[2])-3)}'
    >
    > Or:
    >
    > sed '/:$/{N;s/\n//;}' j | perl -e '
    > while (<>)
    > {
    > next unless $_ =~ m{^CPU Temp \(AMD\):\s+\+(\d+)..C\s+};
    > print "$1\n";
    > }
    > '
    >
    > When you copy into a script you might lose some quotes - make sure
    > they are all there and watch for newlines in wrong place.
    >
    > You could also do the line joining in perl instead of sed.
    >


    Thanks Harry,

    I've actually managed to get this done with AWK with the help of a pile of
    docs and a collegue from work. See my other post
     
    The Hobbit, Sep 16, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin_476
    Replies:
    0
    Views:
    490
    kevin_476
    Feb 13, 2007
  2. FallenPheonix

    Trying to recover some data

    FallenPheonix, Oct 30, 2007, in forum: Computer Support
    Replies:
    1
    Views:
    457
  3. Don Culp

    Need grep

    Don Culp, May 15, 2008, in forum: Windows 64bit
    Replies:
    8
    Views:
    606
    Allan
    May 16, 2008
  4. chuckcar

    EXCEL Trying to format some data

    chuckcar, Apr 1, 2009, in forum: Computer Support
    Replies:
    9
    Views:
    511
    wisdomkiller & pain
    Apr 4, 2009
  5. puja sonwane

    ps -ef | grep "some running process"

    puja sonwane, Apr 1, 2009, in forum: Software
    Replies:
    0
    Views:
    2,393
    puja sonwane
    Apr 1, 2009
Loading...

Share This Page