Find the answer to your Linux question:
Results 1 to 6 of 6
Hey, I'm trying to read a certain value from a power meter. The end result should be that i can read it with snmp from a monitoring machine. When i ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Posts
    4

    Strip output


    Hey,

    I'm trying to read a certain value from a power meter. The end result should be that i can read it with snmp from a monitoring machine.

    When i now do following command on the power meter:
    Code:
    cat /dev/ttyS0
    I recieve following output:
    Code:
    pwr 1151f07babce795d244a11e389ae0a36:0000000000
    pwr 285afa070a3fd4a5f2a69f55e0b8007c:0000000801
    pwr 1151f07babce795d244a11e389ae0a36:0000000000
    pwr 285afa070a3fd4a5f2a69f55e0b8007c:0000000801
    pwr 1151f07babce795d244a11e389ae0a36:0000000000
    pwr 285afa070a3fd4a5f2a69f55e0b8007c:0000000801
    pwr 1151f07babce795d244a11e389ae0a36:0000000000
    pls 285afa070a3fd4a5f2a69f55e0b8007c:0006413740
    pwr 285afa070a3fd4a5f2a69f55e0b8007c:0000000798
    pwr 1151f07babce795d244a11e389ae0a36:0000000000
    pwr 285afa070a3fd4a5f2a69f55e0b8007c:0000000802
    pwr 1151f07babce795d244a11e389ae0a36:0000000000
    pwr 285afa070a3fd4a5f2a69f55e0b8007c:0000000803
    pwr 1151f07babce795d244a11e389ae0a36:0000000000
    pwr 285afa070a3fd4a5f2a69f55e0b8007c:0000000800
    pwr 1151f07babce795d244a11e389ae0a36:0000000000
    pls 285afa070a3fd4a5f2a69f55e0b8007c:0006413741
    .......
    My problem is that i only need to have the pwr values behind 7C. So in this list, this would be:
    Code:
    801
    801
    801
    798
    802
    803
    800


    So i should have a script that returns only the lines with pwr 285afa070a3fd4a5f2a69f55e0b8007c, and that i only recieve the part behind the :


    Anybody can help me out with this?

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,387
    Try this:
    Code:
    cat /dev/ttyS0  | sed -n 's/^pwr 285afa070a3fd4a5f2a69f55e0b8007c:\(0*\)\(.*\)/\2/p'
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Nov 2005
    Posts
    4
    Great,

    It's working!!!
    I see nicely the output now...

    Is there a way to stop it once he has read 1 value?
    Because he keeps going on till i hit ctrl-c...
    Last edited by brononi; 06-14-2011 at 03:51 PM.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    maybe there's a way to do it in sed, but off the top of my head, i'd say pipe it to, well, to head, e.g.

    Code:
    cat /dev/ttyS0 | sed .... | head -n1
    Using head here will just show the first n (in this case, 1) lines of output.

  5. #5
    Just Joined!
    Join Date
    Nov 2005
    Posts
    4
    this isn't working: cat /dev/ttyS0 | sed .... | head -n1

    But this is: cat /dev/ttyS0 | head -n1 | sed ....

    In the second way, i'm not 100% sure that it's correct. It waits a while before it gives the result. But at first sight, it's correct. Or is it just luck that it sends back the correct value? :$

    ps any reason why this is working in case B, but not in case A?

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    If I were you, i'd put that 2nd command in an infinite loop (or finite, depending on what you want to do).

    Code:
    while :; do
      val=$(cat /dev/ttyS0 | head -n1 | sed ....)
      if [ -n "$val" ]; then
        echo "doing something with $val ... "
      else
        echo "nothing to do..."
      fi
      sleep 1
    done

Posting Permissions

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