Re: Standard output piping problem

From: Netocrat (netocrat_at_dodo.com.au)
Date: 05/29/05


Date: Sat, 28 May 2005 22:00:08 GMT

Alan Connor wrote:

> On comp.unix.shell, in <4298d592@news.comindico.com.au>,
> "Netocrat" wrote:
>
>> Alan Connor wrote:
>>
>>> <snip>
>>>
>>> I played with it a bit...
>>>
>>> #!/bin/sh
>>>
>>> while true ; do /usr/sbin/pppstats -c 1 | sed '/IN/d' | \
>>>
>>> ......
>>>
>>> sleep 1 ; done
>>>
>>>
>>> HTH
>>>
>>> AC
>>
>> There are two problems with that: 1) I wanted to avoid running
>> multiple pppstats processes - it offers continuous output so
>> I'd prefer to take advantage of that.
>
> Why do you want to avoid multiple processes that just die in
> a blink?

Call me a perfectionist if you will but I'd rather avoid the overhead of
unecessary process creation.

> You'll be calling many of them in the script itself...

Actually I don't plan on doing that.
 
> 2) On all but the first
>> output, pppstats gives differential stats which saves me having
>> to perform arithmetic since that's the data I'm after.
>>
>> Let me come clean on my full approach: I'm trying to generate
>> an input stream in the form of a series of lines, each with
>> two numbers separated by a space. The first number is bytes
>> received over the interval and the second is bytes sent.
>>
>> So basically this script achieves it: #!/bin/sh
>> /usr/sbin/pppstats -w 1 | sed "{s/\([[:space:]]*\([0-9]*\).*
>> [[:space:]]*\([0-9]*\).*\)/\2 \3/}"
>
> That doesn't work.

My composer cut off a pipe character in the middle of the sed script. Try
this instead:

#!/bin/sh
/usr/sbin/pppstats -w 1 |
sed "s/\([[:space:]]*\([0-9]*\).*|[[:space:]]*\([0-9]*\).*\)/\2 \3/"

I get the following output:

262631059 180722327
4132 3978
1776 144
1259 1624
382 624
0 0
1278 200
468 1604
1548 814
1236 40
543 1636
1584 144
2766 1348
5561 1990
2144 140
3434 1740
1560 52
2687 204
3056 866
1878 1708

3265 208
2741 156

Note that the first line is blank and that there is a blank line after about
20 lines. These I wanted to remove by piping through:
grep -v ^[[:space:]]$

>> Cool, but the problems are (1) I have to ignore the first line
>> since it isn't a differential - no big deal (2) Occasionally
>> pppstats regenerates headers presumably based on its
>> understanding of when a new page occurs on your terminal. This
>> results in a blank line (actually a line comprised solely of a
>> space). No big deal, I thought, I'll filter it out with grep.
>> However as you can see in my reply to Barry, grep piped to sed
>> or vice versa results in no output.
>
> Yes. I know. That's why I came up with the solution I did.
>
>> So that's the real issue.
>> Where in the pipes is the stream blocking?
>>
>
> Don't know. You can pipe it to Cat -A and the output is just
> what one would expect.

Not for my script. Let's call the script above pppmon. Now with:
$ pppmon | cat -A

I get no results.

> I'm thinking it has to do with null-terminated lines and
> that printf might solve the problem.

Yes my thoughts were about perhaps line termination problems too. How do
you propose to use printf to solve the problem?

> Why do you enclose the sed scripts in {} ?

>From the manpage:
sed [OPTION]... {script-only-if-no-other-script} [input-file]...

That seems to indicate it's required. It seems to work without it, but I
took it from the manpage.