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 ...
- 06-12-2011 #1Just 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:
I recieve following output:Code:cat /dev/ttyS0
My problem is that i only need to have the pwr values behind 7C. So in this list, this would be: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 .......
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?
- 06-12-2011 #2
Try this:
Code:cat /dev/ttyS0 | sed -n 's/^pwr 285afa070a3fd4a5f2a69f55e0b8007c:\(0*\)\(.*\)/\2/p'You must always face the curtain with a bow.
- 06-14-2011 #3Just 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.
- 06-16-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
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.
Using head here will just show the first n (in this case, 1) lines of output.Code:cat /dev/ttyS0 | sed .... | head -n1
- 06-16-2011 #5Just 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?
- 06-16-2011 #6Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
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


Reply With Quote